Deallocate memory before setting new value
[platform/core/uifw/voice-control.git] / common / vc_cmd_db.c
1 /*
2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <app_manager.h>
18 #include <db-util.h>
19 #include <dlog.h>
20 #include <sqlite3.h>
21 #include <tzplatform_config.h>
22
23 #include "vc_cmd_db.h"
24 #include "vc_main.h"
25 #include "vc_command.h"
26 #include "voice_control_command_expand.h"
27
28
29 const char* vc_db_tag()
30 {
31         return TAG_VCDB;
32 }
33
34 /*!
35  * \note
36  * DB Table schema
37  *
38  * vc_info
39  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
40  * | id  | pid  | type | format | domain | command | parameter | appid | invocation_name | fixed |
41  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
42  *
43  * CREATE TABLE vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, appid TEXT);
44  *
45  * vc_result
46  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
47  * | id  | result | event | msg | exclusive | pid | type | format | domain | command | parameter | appid | invocation_name | fixed |
48  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
49  *
50  * CREATE TABLE vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,
51  *                                      pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT,\
52  *
53  */
54
55 #define FREE_MEM(X) {if (NULL != X) {free(X); X = NULL; } }
56
57
58 //#define DB_PATH tzplatform_mkpath(TZ_USER_DB, ".vc_info.db")
59 static sqlite3* g_db_handle = NULL;
60 static sqlite3* g_db_backup_handle = NULL;
61 static char* g_path = NULL;
62 static char* g_backup_path = NULL;
63 static int g_ref_cnt = 0;
64
65 int g_fpid = -1;
66 int g_db_cnt = 0;
67 int g_backup_db_cnt = 0;
68
69 static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist);
70 static int __vc_db_begin_transaction(sqlite3* db_handle);
71 static int __vc_db_create_table(sqlite3* db_handle, const char* table);
72 static int __vc_db_rollback_transaction(sqlite3* db_handle);
73 static int __vc_db_commit_transaction(sqlite3* db_handle);
74
75
76 int __vc_db_reset_handle(void)
77 {
78         if (!g_db_handle || !g_db_backup_handle) {
79                 vc_db_finalize();
80                 vc_db_initialize();
81         }
82         return VC_DB_ERROR_NONE;
83 }
84
85 static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
86 {
87         sqlite3_stmt* pStmt = NULL;
88         int ret = -1;
89
90         /* prepare db */
91         ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
92         if (ret != SQLITE_OK) {
93                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
94                 return VC_DB_ERROR_OPERATION_FAILED;
95         }
96
97         if (sqlite3_step(pStmt) != SQLITE_DONE) {
98                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
99                 sqlite3_finalize(pStmt);
100                 return VC_DB_ERROR_OPERATION_FAILED;
101         }
102
103         sqlite3_finalize(pStmt);
104         return VC_DB_ERROR_NONE;
105 }
106
107 static int __vc_db_begin_transaction(sqlite3* db_handle)
108 {
109         int ret = __vc_db_transaction(db_handle, "BEGIN IMMEDIATE TRANSACTION");
110         return ret;
111 }
112
113 static int __vc_db_rollback_transaction(sqlite3* db_handle)
114 {
115         int ret = __vc_db_transaction(db_handle, "ROLLBACK TRANSACTION");
116         return ret;
117 }
118
119 static int __vc_db_commit_transaction(sqlite3* db_handle)
120 {
121         int ret = __vc_db_transaction(db_handle, "COMMIT TRANSACTION");
122         return ret;
123 }
124
125 static int __vc_db_exec_query(sqlite3* db_handle, const char* sql)
126 {
127         char* err_msg = NULL;
128
129         int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
130         if (ret != SQLITE_OK) {
131                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
132                 sqlite3_free(err_msg);
133                 err_msg = NULL;
134                 return VC_DB_ERROR_OPERATION_FAILED;
135         }
136         return VC_DB_ERROR_NONE;
137 }
138
139 static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist)
140 {
141         char* sql = NULL;
142         *is_exist = false;
143         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
144                 sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_result';");
145         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
146                 sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_info';");
147         } else {
148                 return VC_DB_ERROR_INVALID_PARAMETER;
149         }
150
151         if (NULL == sql) {
152                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
153                 return VC_DB_ERROR_OUT_OF_MEMORY;
154         }
155
156         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
157
158         char* err_msg = NULL;
159         char** results = NULL;
160         int ret = sqlite3_get_table(db_handle, sql, &results, NULL, NULL, &err_msg);
161         if (ret != SQLITE_OK) {
162                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_get_table return fail, ret(%d), err(%s)", ret, err_msg);
163                 sqlite3_free(err_msg);
164                 err_msg = NULL;
165                 return VC_DB_ERROR_OPERATION_FAILED;
166         }
167
168         if (0 != atoi(results[1])) {
169                 *is_exist = true;
170                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] table exists (%d)", atoi(results[1]));
171         } else {
172                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] table doesn't exist (%d)", atoi(results[1]));
173         }
174
175         sqlite3_free_table(results);
176         free(sql);
177         sql = NULL;
178         return VC_DB_ERROR_NONE;
179 }
180
181 static int __vc_db_delete_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, const char* appid)
182 {
183         sqlite3_stmt* stmt = NULL;
184         char* sql = NULL;
185
186         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid)
187                 sql = strdup("DELETE FROM vc_info WHERE type = ?;");
188         else if (NULL != appid)
189                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND appid = ?;");
190         else
191                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
192
193         if (NULL == sql) {
194                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
195                 return VC_DB_ERROR_OUT_OF_MEMORY;
196         }
197
198         int ret = 0;
199         /* check whether there is a table or not */
200         bool is_exist = false;
201         ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
202         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
203                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
204                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
205                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
206                         free(sql);
207                         sql = NULL;
208                         return VC_DB_ERROR_OPERATION_FAILED;
209                 }
210         } else {
211                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
212         }
213
214         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
215         if (ret != SQLITE_OK) {
216                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
217                 free(sql);
218                 sql = NULL;
219                 return VC_DB_ERROR_OPERATION_FAILED;
220         }
221         ret = sqlite3_bind_int(stmt, 1, (int)type);
222         if (ret != SQLITE_OK) {
223                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
224                 sqlite3_finalize(stmt);
225                 free(sql);
226                 sql = NULL;
227                 return VC_DB_ERROR_OPERATION_FAILED;
228         }
229
230         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid) {
231                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d;", type);
232         } else if (NULL != appid) {
233                 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
234                 if (ret != SQLITE_OK) {
235                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
236                         sqlite3_clear_bindings(stmt);
237                         sqlite3_finalize(stmt);
238                         free(sql);
239                         sql = NULL;
240                         return VC_DB_ERROR_OPERATION_FAILED;
241                 }
242                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
243         } else  {
244                 ret = sqlite3_bind_int(stmt, 2, pid);
245                 if (ret != SQLITE_OK) {
246                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
247                         sqlite3_clear_bindings(stmt);
248                         sqlite3_finalize(stmt);
249                         free(sql);
250                         sql = NULL;
251                         return VC_DB_ERROR_OPERATION_FAILED;
252                 }
253                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
254         }
255
256         ret = sqlite3_step(stmt);
257         if (ret != SQLITE_DONE) {
258                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned, sql(%s), ret(%d), err(%s)", sql, ret, sqlite3_errmsg(db_handle));
259                 sqlite3_clear_bindings(stmt);
260                 sqlite3_finalize(stmt);
261                 free(sql);
262                 sql = NULL;
263                 return VC_DB_ERROR_OPERATION_FAILED;
264         }
265
266         sqlite3_reset(stmt);
267         sqlite3_clear_bindings(stmt);
268         sqlite3_finalize(stmt);
269
270         free(sql);
271         sql = NULL;
272         return VC_DB_ERROR_NONE;
273 }
274
275 static int __vc_db_insert_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
276 {
277         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
278
279         sqlite3_stmt* stmt = NULL;
280         const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
281         int ret = 0;
282         /* check whether there is a table or not */
283         bool is_exist = false;
284         ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
285         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
286                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
287                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
288                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
289                         return VC_DB_ERROR_OPERATION_FAILED;
290                 }
291         } else {
292                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
293         }
294
295         /* prepare db */
296         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
297         if (ret != SQLITE_OK) {
298                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
299                 return VC_DB_ERROR_OPERATION_FAILED;
300         }
301         ret = sqlite3_bind_int(stmt, 2, pid);
302         if (ret != SQLITE_OK) {
303                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
304                 sqlite3_finalize(stmt);
305                 return VC_DB_ERROR_OPERATION_FAILED;
306         }
307         ret = sqlite3_bind_int(stmt, 3, cmd->type);
308         if (ret != SQLITE_OK) {
309                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
310                 sqlite3_clear_bindings(stmt);
311                 sqlite3_finalize(stmt);
312                 return VC_DB_ERROR_OPERATION_FAILED;
313         }
314         ret = sqlite3_bind_int(stmt, 4, cmd->format);
315         if (ret != SQLITE_OK) {
316                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
317                 sqlite3_clear_bindings(stmt);
318                 sqlite3_finalize(stmt);
319                 return VC_DB_ERROR_OPERATION_FAILED;
320         }
321         ret = sqlite3_bind_int(stmt, 5, cmd->domain);
322         if (ret != SQLITE_OK) {
323                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
324                 sqlite3_clear_bindings(stmt);
325                 sqlite3_finalize(stmt);
326                 return VC_DB_ERROR_OPERATION_FAILED;
327         }
328         ret = sqlite3_bind_text(stmt, 6, cmd->command, -1, SQLITE_TRANSIENT);
329         if (ret != SQLITE_OK) {
330                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
331                 sqlite3_clear_bindings(stmt);
332                 sqlite3_finalize(stmt);
333                 return VC_DB_ERROR_OPERATION_FAILED;
334         }
335
336         char* appid = NULL;
337         if (NULL == cmd->appid) {
338                 // Get appid by pid using app control
339                 ret = app_manager_get_app_id(pid, &appid);
340                 if (APP_MANAGER_ERROR_NONE != ret) {
341                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
342                 }
343                 if (NULL != appid) {
344                         cmd->appid = strdup(appid);
345                         FREE_MEM(appid);
346                 }
347         }
348         ret = sqlite3_bind_text(stmt, 8, cmd->appid, -1, SQLITE_TRANSIENT);
349         if (ret != SQLITE_OK) {
350                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
351                 sqlite3_clear_bindings(stmt);
352                 sqlite3_finalize(stmt);
353                 return VC_DB_ERROR_OPERATION_FAILED;
354         }
355         if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
356                 ret = sqlite3_bind_text(stmt, 9, cmd->invocation_name, -1, SQLITE_TRANSIENT);
357                 if (ret != SQLITE_OK) {
358                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
359                         sqlite3_clear_bindings(stmt);
360                         sqlite3_finalize(stmt);
361                         return VC_DB_ERROR_OPERATION_FAILED;
362                 }
363         }
364         ret = sqlite3_bind_text(stmt, 10, cmd->fixed, -1, SQLITE_TRANSIENT);
365         if (ret != SQLITE_OK) {
366                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
367                 sqlite3_clear_bindings(stmt);
368                 sqlite3_finalize(stmt);
369                 return VC_DB_ERROR_OPERATION_FAILED;
370         }
371         ret = sqlite3_step(stmt);
372         if (ret != SQLITE_DONE) {
373                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
374                 sqlite3_clear_bindings(stmt);
375                 sqlite3_finalize(stmt);
376                 return VC_DB_ERROR_OPERATION_FAILED;
377         }
378
379         SLOG(LOG_INFO, vc_db_tag(), "[SQL] INSERT INTO vc_info (id, pid(%d), type(%d), format(%d), domain(%d), command(%s)", pid, cmd->type, cmd->format, cmd->domain, cmd->command);
380         SLOG(LOG_INFO, vc_db_tag(), "[SQL] @@ appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
381
382         sqlite3_reset(stmt);
383         sqlite3_clear_bindings(stmt);
384         sqlite3_finalize(stmt);
385         return VC_DB_ERROR_NONE;
386 }
387
388 static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList** cmd_list)
389 {
390         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
391
392         int ret = 0;
393         sqlite3_stmt* stmt = NULL;
394         char* sql = NULL;
395         char* appid = NULL;
396
397         if (VC_COMMAND_TYPE_BACKGROUND == type) {
398                 /* For background command */
399                 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
400         } else {
401                 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
402         }
403
404         if (NULL == sql) {
405                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
406                 return VC_DB_ERROR_OUT_OF_MEMORY;
407         }
408
409         /* check whether there is a table or not */
410         bool is_exist = false;
411         ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
412         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
413                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
414                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
415                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
416                         free(sql);
417                         sql = NULL;
418                         return VC_DB_ERROR_OPERATION_FAILED;
419                 }
420         } else {
421                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
422         }
423
424         /* prepare db */
425         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
426         if (ret != SQLITE_OK) {
427                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
428                 free(sql);
429                 sql = NULL;
430                 return VC_DB_ERROR_OPERATION_FAILED;
431         }
432
433         ret = sqlite3_bind_int(stmt, 1, (int)type);
434         if (ret != SQLITE_OK) {
435                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
436                 sqlite3_finalize(stmt);
437                 free(sql);
438                 sql = NULL;
439                 return VC_DB_ERROR_OPERATION_FAILED;
440         }
441         if (VC_COMMAND_TYPE_BACKGROUND != type) {
442                 ret = sqlite3_bind_int(stmt, 2, pid);
443                 if (ret != SQLITE_OK) {
444                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
445                         sqlite3_clear_bindings(stmt);
446                         sqlite3_finalize(stmt);
447                         free(sql);
448                         sql = NULL;
449                         return VC_DB_ERROR_OPERATION_FAILED;
450                 }
451         }
452
453         if (VC_COMMAND_TYPE_BACKGROUND == type)
454                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
455         else
456                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
457
458         ret = sqlite3_step(stmt);
459         if (ret == SQLITE_DONE) {
460                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
461                 sqlite3_clear_bindings(stmt);
462                 sqlite3_finalize(stmt);
463                 free(sql);
464                 sql = NULL;
465                 return VC_DB_ERROR_NONE;
466         }
467
468         if (VC_COMMAND_TYPE_BACKGROUND == type && -1 != pid) {
469                 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
470                         SLOG(LOG_WARN, vc_db_tag(), "[WARN] fail to get app id, pid(%d)", pid);
471                 }
472         }
473
474         while (SQLITE_ROW == ret) {
475                 int temp = 0;
476                 int ret = -1;
477                 char* temp_text = NULL;
478                 vc_cmd_h temp_cmd;
479                 ret = vc_cmd_create(&temp_cmd);
480
481                 if (0 != ret) {
482                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
483                         if (NULL != appid) {
484                                 free(appid);
485                                 appid = NULL;
486                         }
487                         sqlite3_reset(stmt);
488                         sqlite3_clear_bindings(stmt);
489                         sqlite3_finalize(stmt);
490                         free(sql);
491                         sql = NULL;
492                         return VC_DB_ERROR_OUT_OF_MEMORY;
493                 }
494
495                 temp_text = (char*)sqlite3_column_text(stmt, 7);
496                 if (NULL != temp_text)
497                         vc_cmd_set_appid(temp_cmd, temp_text);
498
499                 ret = vc_cmd_get_appid(temp_cmd, &temp_text);
500                 if (NULL != appid && 0 == ret) {
501                         if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_text, strlen(appid))) {
502                                 SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
503
504                                 free(temp_text);
505                                 temp_text = NULL;
506
507                                 vc_cmd_destroy(temp_cmd);
508                                 temp_cmd = NULL;
509
510                                 ret = sqlite3_step(stmt);
511                                 if (SQLITE_DONE == ret)
512                                         break;
513
514                                 continue;
515                         }
516                 }
517
518                 if (NULL != temp_text) {
519                         free(temp_text);
520                         temp_text = NULL;
521                 }
522
523                 temp = sqlite3_column_int(stmt, 0);
524                 vc_cmd_set_id(temp_cmd, temp);
525
526                 temp = sqlite3_column_int(stmt, 1);
527                 vc_cmd_set_pid(temp_cmd, temp);
528
529                 temp = sqlite3_column_int(stmt, 2);
530                 vc_cmd_set_type(temp_cmd, temp);
531
532                 temp = sqlite3_column_int(stmt, 3);
533                 vc_cmd_set_format(temp_cmd, temp);
534
535                 temp  = sqlite3_column_int(stmt, 4);
536                 vc_cmd_set_domain(temp_cmd, temp);
537
538                 temp_text = (char*)sqlite3_column_text(stmt, 5);
539                 if (NULL != temp_text)
540                         vc_cmd_set_command(temp_cmd, temp_text);
541
542                 temp_text = (char*)sqlite3_column_text(stmt, 6);
543                 if (NULL != temp_text)
544                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
545
546                 temp_text = (char*)sqlite3_column_text(stmt, 8);
547                 if (NULL != temp_text)
548                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
549
550                 temp_text = (char*)sqlite3_column_text(stmt, 9);
551                 if (NULL != temp_text)
552                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
553
554                 ret = vc_cmd_get_type(temp_cmd, &temp);
555                 if (0 == ret && type == temp) {
556                         *cmd_list = g_slist_append(*cmd_list, temp_cmd);
557                 } else {
558                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp, type);
559
560                         vc_cmd_destroy(temp_cmd);
561                         temp_cmd = NULL;
562                 }
563                 ret = sqlite3_step(stmt);
564                 if (SQLITE_DONE == ret)
565                         break;
566         }
567
568         if (NULL != appid) {
569                 free(appid);
570                 appid = NULL;
571         }
572
573         sqlite3_reset(stmt);
574         sqlite3_clear_bindings(stmt);
575         sqlite3_finalize(stmt);
576
577         free(sql);
578         sql = NULL;
579         return VC_DB_ERROR_NONE;
580 }
581
582 static int __vc_db_get_pid(const char* appid, int* pid)
583 {
584         bool running = false;
585         int ret = app_manager_is_running(appid, &running);
586         if (APP_MANAGER_ERROR_NONE != ret) {
587                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to check running with appid(%s)", appid);
588                 return VC_DB_ERROR_OPERATION_FAILED;
589         }
590         if (true == running) {
591                 app_context_h app_context = NULL;
592                 ret = app_manager_get_app_context(appid, &app_context);
593                 if (APP_MANAGER_ERROR_NONE != ret) {
594                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
595                         return VC_DB_ERROR_OPERATION_FAILED;
596                 }
597
598                 ret = app_context_get_pid(app_context, pid);
599                 int destroy_ret = app_context_destroy(app_context);
600                 if (APP_MANAGER_ERROR_NONE != destroy_ret) {
601                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to destroy app context, ret(%d), appid(%s)", destroy_ret, appid);
602                 }
603
604                 if (APP_MANAGER_ERROR_NONE != ret) {
605                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get pid, ret(%d), appid(%s)", ret, appid);
606                         return VC_DB_ERROR_OPERATION_FAILED;
607                 }
608         } else {
609                 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
610         }
611         return VC_DB_ERROR_NONE;
612 }
613
614 static int __vc_db_insert_result(sqlite3* db_handle, const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
615 {
616         SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
617
618         sqlite3_stmt* stmt = NULL;
619         const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
620         int ret = 0;
621
622         /* check whether there is a table or not */
623         bool is_exist = false;
624         ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
625         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
626                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
627                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
628                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
629                         return VC_DB_ERROR_OPERATION_FAILED;
630                 }
631         } else {
632                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
633         }
634
635         /* prepare db */
636         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
637         if (ret != SQLITE_OK) {
638                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
639                 return VC_DB_ERROR_OPERATION_FAILED;
640         }
641         ret = sqlite3_bind_text(stmt, 2, result_text, -1, SQLITE_TRANSIENT);
642         if (ret != SQLITE_OK) {
643                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
644                 sqlite3_finalize(stmt);
645                 return VC_DB_ERROR_OPERATION_FAILED;
646         }
647         ret = sqlite3_bind_int(stmt, 3, event);
648         if (ret != SQLITE_OK) {
649                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
650                 sqlite3_clear_bindings(stmt);
651                 sqlite3_finalize(stmt);
652                 return VC_DB_ERROR_OPERATION_FAILED;
653         }
654         ret = sqlite3_bind_text(stmt, 4, msg, -1, SQLITE_TRANSIENT);
655         if (ret != SQLITE_OK) {
656                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
657                 sqlite3_clear_bindings(stmt);
658                 sqlite3_finalize(stmt);
659                 return VC_DB_ERROR_OPERATION_FAILED;
660         }
661         ret = sqlite3_bind_int(stmt, 5, exclusive);
662         if (ret != SQLITE_OK) {
663                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
664                 sqlite3_clear_bindings(stmt);
665                 sqlite3_finalize(stmt);
666                 return VC_DB_ERROR_OPERATION_FAILED;
667         }
668
669         if (NULL == cmd) {
670                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d))", result_text, event, msg, exclusive);
671
672                 ret = sqlite3_step(stmt);
673                 if (ret != SQLITE_DONE) {
674                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
675                         sqlite3_clear_bindings(stmt);
676                         sqlite3_finalize(stmt);
677                         return VC_DB_ERROR_OPERATION_FAILED;
678                 }
679                 sqlite3_reset(stmt);
680                 sqlite3_clear_bindings(stmt);
681                 sqlite3_finalize(stmt);
682                 return VC_DB_ERROR_NONE;
683         }
684
685         if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
686                 int pid = -1;
687                 ret = __vc_db_get_pid(cmd->appid, &pid);
688                 if (0 != ret) {
689                         SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
690                 } else {
691                         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
692                         if (-1 != pid)
693                                 cmd->pid = pid;
694                 }
695         }
696         ret = sqlite3_bind_int(stmt, 6, cmd->pid);
697         if (ret != SQLITE_OK) {
698                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
699                 sqlite3_clear_bindings(stmt);
700                 sqlite3_finalize(stmt);
701                 return VC_DB_ERROR_OPERATION_FAILED;
702         }
703         ret = sqlite3_bind_int(stmt, 7, cmd->type);
704         if (ret != SQLITE_OK) {
705                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
706                 sqlite3_clear_bindings(stmt);
707                 sqlite3_finalize(stmt);
708                 return VC_DB_ERROR_OPERATION_FAILED;
709         }
710         ret = sqlite3_bind_int(stmt, 8, cmd->format);
711         if (ret != SQLITE_OK) {
712                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
713                 sqlite3_clear_bindings(stmt);
714                 sqlite3_finalize(stmt);
715                 return VC_DB_ERROR_OPERATION_FAILED;
716         }
717         ret = sqlite3_bind_int(stmt, 9, cmd->domain);
718         if (ret != SQLITE_OK) {
719                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
720                 sqlite3_clear_bindings(stmt);
721                 sqlite3_finalize(stmt);
722                 return VC_DB_ERROR_OPERATION_FAILED;
723         }
724         ret = sqlite3_bind_text(stmt, 10, cmd->command, -1, SQLITE_TRANSIENT);
725         if (ret != SQLITE_OK) {
726                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
727                 sqlite3_clear_bindings(stmt);
728                 sqlite3_finalize(stmt);
729                 return VC_DB_ERROR_OPERATION_FAILED;
730         }
731         ret = sqlite3_bind_text(stmt, 11, cmd->parameter, -1, SQLITE_TRANSIENT);
732         if (ret != SQLITE_OK) {
733                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
734                 sqlite3_clear_bindings(stmt);
735                 sqlite3_finalize(stmt);
736                 return VC_DB_ERROR_OPERATION_FAILED;
737         }
738         ret = sqlite3_bind_text(stmt, 12, cmd->appid, -1, SQLITE_TRANSIENT);
739         if (ret != SQLITE_OK) {
740                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
741                 sqlite3_clear_bindings(stmt);
742                 sqlite3_finalize(stmt);
743                 return VC_DB_ERROR_OPERATION_FAILED;
744         }
745         ret = sqlite3_bind_text(stmt, 13, cmd->invocation_name, -1, SQLITE_TRANSIENT);
746         if (ret != SQLITE_OK) {
747                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
748                 sqlite3_clear_bindings(stmt);
749                 sqlite3_finalize(stmt);
750                 return VC_DB_ERROR_OPERATION_FAILED;
751         }
752         ret = sqlite3_bind_text(stmt, 14, cmd->fixed, -1, SQLITE_TRANSIENT);
753         if (ret != SQLITE_OK) {
754                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
755                 sqlite3_clear_bindings(stmt);
756                 sqlite3_finalize(stmt);
757                 return VC_DB_ERROR_OPERATION_FAILED;
758         }
759         ret = sqlite3_step(stmt);
760         if (ret != SQLITE_DONE) {
761                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
762                 sqlite3_clear_bindings(stmt);
763                 sqlite3_finalize(stmt);
764                 return VC_DB_ERROR_OPERATION_FAILED;
765         }
766
767         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d), pid(%d), type(%d), format(%d), domain(%d), command(%s), parameter(%s), appid(%s), invocation(%s), fixed(%s)",
768                                 result_text, event, msg, exclusive, cmd->pid, cmd->type, cmd->format, cmd->domain, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
769
770         sqlite3_reset(stmt);
771         sqlite3_clear_bindings(stmt);
772         sqlite3_finalize(stmt);
773
774         return VC_DB_ERROR_NONE;
775 }
776
777 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
778 {
779         if (NULL == org_cmd || NULL == new_cmd) {
780                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
781                 return VC_DB_ERROR_INVALID_PARAMETER;
782         }
783
784         if (NULL == invocation_name) {
785                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, invocation name is NULL, org_cmd(%s)", org_cmd);
786                 return VC_DB_ERROR_INVALID_PARAMETER;
787         }
788
789         if (strlen(org_cmd) <= strlen(invocation_name)) {
790                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
791                 return VC_DB_ERROR_INVALID_PARAMETER;
792         }
793         if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
794                 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
795         }
796         SLOG(LOG_INFO, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, (*new_cmd) ? *new_cmd : "NULL", invocation_name);
797         return VC_DB_ERROR_NONE;
798 }
799
800 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
801 {
802         if (NULL == command || NULL == temp_unfixed) {
803                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
804                 return VC_DB_ERROR_INVALID_PARAMETER;
805         }
806
807         if (NULL == fixed) {
808                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, fixed cmd is NULL, org_cmd(%s)", command);
809                 return VC_DB_ERROR_INVALID_PARAMETER;
810         }
811
812         if (strlen(command) <= strlen(fixed)) {
813                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to extract unfixed command, cmd(%s) fixed(%s)", command, fixed);
814                 return VC_DB_ERROR_INVALID_PARAMETER;
815         }
816
817         char* temp = (char*)calloc(256, sizeof(char));
818         if (NULL == temp) {
819                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
820                 return VC_DB_ERROR_OUT_OF_MEMORY;
821         }
822         if (0 == strncasecmp(command, fixed, strlen(fixed))) {
823                 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
824                 SLOG(LOG_WARN, vc_db_tag(), "@@@");
825         } else  if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
826                 command[strlen(command) - strlen(fixed) - 1] = '\0';
827                 snprintf(temp, 256, "%s", command);
828                 SLOG(LOG_WARN, vc_db_tag(), "@@@");
829         }
830
831         SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
832         if (NULL != temp) {
833                 *temp_unfixed = strdup(temp);
834                 free(temp);
835                 temp = NULL;
836         }
837         return VC_DB_ERROR_NONE;
838 }
839
840 static int __vc_db_get_result(sqlite3* db_handle, char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
841 {
842         int ret = 0;
843         sqlite3_stmt* stmt = NULL;
844         char* sql = NULL;
845         if (-1 == pid)
846                 sql = strdup("SELECT * FROM vc_result;");
847         else if (NULL != appid)
848                 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
849         else
850                 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
851
852         if (NULL == sql) {
853                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
854                 return VC_DB_ERROR_OUT_OF_MEMORY;
855         }
856
857         /* check whether there is a table or not */
858         bool is_exist = false;
859         ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
860         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
861                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
862                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
863                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
864                         free(sql);
865                         sql = NULL;
866                         return VC_DB_ERROR_OPERATION_FAILED;
867                 }
868         } else {
869                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
870         }
871
872         /* prepare db */
873         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
874         if (ret != SQLITE_OK) {
875                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
876                 free(sql);
877                 sql = NULL;
878                 return VC_DB_ERROR_OPERATION_FAILED;
879         }
880         if (NULL != appid) {
881                 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
882                 if (ret != SQLITE_OK) {
883                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
884                         sqlite3_finalize(stmt);
885                         free(sql);
886                         sql = NULL;
887                         return VC_DB_ERROR_OPERATION_FAILED;
888                 }
889         } else if (-1 != pid) {
890                 ret = sqlite3_bind_int(stmt, 1, pid);
891                 if (ret != SQLITE_OK) {
892                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
893                         sqlite3_finalize(stmt);
894                         free(sql);
895                         sql = NULL;
896                         return VC_DB_ERROR_OPERATION_FAILED;
897                 }
898         }
899         ret = sqlite3_step(stmt);
900         if (ret == SQLITE_DONE) {
901                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
902                 sqlite3_clear_bindings(stmt);
903                 sqlite3_finalize(stmt);
904                 free(sql);
905                 sql = NULL;
906                 return VC_DB_ERROR_NONE;
907         }
908
909         if (-1 == pid)
910                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
911         else if (NULL != appid)
912                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %s;", appid);
913
914         else
915                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
916
917         vc_cmd_h temp_cmd = NULL;
918         while (SQLITE_ROW == ret) {
919                 int temp = 0;
920                 char* temp_text = NULL;
921                 const char* invocation_name = NULL;
922
923                 temp_text = (char*)sqlite3_column_text(stmt, 1);
924                 if (NULL != temp_text)
925                         *result_text = strdup(temp_text);
926
927                 temp = sqlite3_column_int(stmt, 2);
928                 *event = temp;
929
930                 if (NULL != msg) {
931                         temp_text = (char*)sqlite3_column_text(stmt, 3);
932                         if (NULL != temp_text)
933                                 *msg = strdup(temp_text);
934                 }
935
936                 if (0 != vc_cmd_create(&temp_cmd)) {
937                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
938                         if (NULL != *result_text) {
939                                 free(*result_text);
940                                 *result_text = NULL;
941                         }
942
943                         if (NULL != msg && NULL != *msg) {
944                                 free(*msg);
945                                 *msg = NULL;
946                         }
947
948                         sqlite3_reset(stmt);
949                         sqlite3_clear_bindings(stmt);
950                         sqlite3_finalize(stmt);
951                         free(sql);
952                         sql = NULL;
953                         return -1;
954                 }
955
956                 temp = sqlite3_column_int(stmt, 0);
957                 vc_cmd_set_id(temp_cmd, temp);
958
959                 temp = sqlite3_column_int(stmt, 5);
960                 vc_cmd_set_pid(temp_cmd, temp);
961
962                 temp = sqlite3_column_int(stmt, 6);
963                 vc_cmd_set_type(temp_cmd, temp);
964
965                 temp = sqlite3_column_int(stmt, 7);
966                 vc_cmd_set_format(temp_cmd, temp);
967
968                 temp = sqlite3_column_int(stmt, 8);
969                 vc_cmd_set_domain(temp_cmd, temp);
970
971                 // invocation name
972                 invocation_name = (char*)sqlite3_column_text(stmt, 12);
973                 // command name
974                 temp_text = (char*)sqlite3_column_text(stmt, 9);
975                 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
976
977                 if (NULL != temp_text) {
978                         char* temp_command = NULL;
979                         char* temp_unfixed = NULL;
980                         char* temp_fixed = NULL;
981
982                         if (NULL != invocation_name)
983                                 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
984
985                         if (NULL == temp_command) {
986                                 temp_command = strdup(temp_text);
987                         } else {
988                                 // remove invocation name from result_text
989                                 free(*result_text);
990                                 *result_text = strdup(temp_command);
991                         }
992
993                         // fixed
994                         temp_fixed = (char*)sqlite3_column_text(stmt, 13);
995                         if (NULL != temp_fixed)
996                                 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
997
998                         // unfixed
999                         temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
1000
1001                         if (NULL != temp_unfixed) {
1002                                 char merge_result[256] = {0, };
1003                                 vc_cmd_set_command(temp_cmd, temp_command);
1004                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
1005                                 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
1006                                 if (NULL != *result_text)
1007                                         free(*result_text);
1008                                 *result_text = strdup(merge_result);
1009                         } else if (NULL != temp_fixed) {
1010                                 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
1011                                 vc_cmd_set_command(temp_cmd, temp_fixed);
1012                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
1013                                 if (NULL != temp_unfixed) {
1014                                         free(temp_unfixed);
1015                                 temp_unfixed = NULL;
1016                                 }
1017                         } else {
1018                                 vc_cmd_set_command(temp_cmd, temp_command);
1019                         }
1020                         free(temp_command);
1021                         temp_command = NULL;
1022                 }
1023
1024                 temp_text = (char*)sqlite3_column_text(stmt, 11);
1025                 if (NULL != temp_text)
1026                         vc_cmd_set_appid(temp_cmd, temp_text);
1027
1028                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1029                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1030                         vc_cmd_destroy(temp_cmd);
1031                         vc_cmd_list_destroy(vc_cmd_list, true);
1032                         vc_cmd_list = NULL;
1033                         if (NULL != *result_text) {
1034                                 free(*result_text);
1035                                 *result_text = NULL;
1036                         }
1037
1038                         if (NULL != msg && NULL != *msg) {
1039                                 free(*msg);
1040                                 *msg = NULL;
1041                         }
1042
1043                         sqlite3_reset(stmt);
1044                         sqlite3_clear_bindings(stmt);
1045                         sqlite3_finalize(stmt);
1046                         free(sql);
1047                         sql = NULL;
1048                         return VC_DB_ERROR_OPERATION_FAILED;
1049                 }
1050
1051                 ret = sqlite3_step(stmt);
1052                 if (SQLITE_DONE == ret)
1053                         break;
1054         }
1055
1056         sqlite3_reset(stmt);
1057         sqlite3_clear_bindings(stmt);
1058         sqlite3_finalize(stmt);
1059
1060         free(sql);
1061         sql = NULL;
1062         return VC_DB_ERROR_NONE;
1063 }
1064
1065 void __vc_db_demandable_client_free(void* data)
1066 {
1067         vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data;
1068
1069         if (NULL != d_app) {
1070                 if (NULL != d_app->appid) {
1071                         free(d_app->appid);
1072                         d_app->appid = NULL;
1073                 }
1074
1075                 free(d_app);
1076         }
1077 }
1078
1079 static int __vc_db_get_appid(sqlite3* db_handle, const char* result, GSList** app_list)
1080 {
1081         GSList* temp_app_list = NULL;
1082
1083         int ret = 0;
1084         sqlite3_stmt* stmt = NULL;
1085         const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
1086
1087         /* check whether there is a table or not */
1088         bool is_exist = false;
1089         ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
1090         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1091                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
1092                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
1093                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1094                         return VC_DB_ERROR_OPERATION_FAILED;
1095                 }
1096         } else {
1097                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
1098         }
1099
1100         /* prepare db */
1101         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1102         if (ret != SQLITE_OK) {
1103                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1104                 return VC_DB_ERROR_OPERATION_FAILED;
1105         }
1106         ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
1107         if (ret != SQLITE_OK) {
1108                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1109                 sqlite3_finalize(stmt);
1110                 return VC_DB_ERROR_OPERATION_FAILED;
1111         }
1112         ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
1113         if (ret != SQLITE_OK) {
1114                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1115                 sqlite3_clear_bindings(stmt);
1116                 sqlite3_finalize(stmt);
1117                 return VC_DB_ERROR_OPERATION_FAILED;
1118         }
1119         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
1120         ret = sqlite3_step(stmt);
1121         if (ret == SQLITE_DONE) {
1122                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1123                 sqlite3_clear_bindings(stmt);
1124                 sqlite3_finalize(stmt);
1125                 return VC_DB_ERROR_NONE;
1126         }
1127
1128         while (SQLITE_ROW == ret) {
1129                 char* temp_text = NULL;
1130                 vc_deactivated_app_s* temp_app = NULL;
1131                 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
1132                 if (NULL == temp_app) {
1133                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allocation fail");
1134
1135                         if (NULL != temp_app_list) {
1136                                 g_slist_free_full(temp_app_list, __vc_db_demandable_client_free);
1137                                 temp_app_list = NULL;
1138                         }
1139
1140                         sqlite3_reset(stmt);
1141                         sqlite3_clear_bindings(stmt);
1142                         sqlite3_finalize(stmt);
1143                         return VC_DB_ERROR_OUT_OF_MEMORY;
1144                 }
1145
1146                 temp_text = (char*)sqlite3_column_text(stmt, 11);
1147                 if (NULL != temp_text)
1148                         temp_app->appid = strdup(temp_text);
1149
1150                 temp_app_list = g_slist_append(temp_app_list, temp_app);
1151
1152                 ret = sqlite3_step(stmt);
1153                 if (SQLITE_DONE == ret)
1154                         break;
1155         }
1156
1157         *app_list = temp_app_list;
1158
1159         sqlite3_reset(stmt);
1160         sqlite3_clear_bindings(stmt);
1161         sqlite3_finalize(stmt);
1162         return VC_DB_ERROR_NONE;
1163 }
1164
1165 int __vc_db_get_result_pid_list(sqlite3* db_handle, const char* result, GSList** pid_list)
1166 {
1167         GSList* temp_pid_list = NULL;
1168
1169         int ret = 0;
1170         sqlite3_stmt* stmt = NULL;
1171         const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
1172
1173
1174         /* check whether there is a table or not */
1175         bool is_exist = false;
1176         ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
1177         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1178                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
1179                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
1180                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1181                         return VC_DB_ERROR_OPERATION_FAILED;
1182                 }
1183         } else {
1184                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
1185         }
1186
1187         /* prepare db */
1188         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1189         if (ret != SQLITE_OK) {
1190                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1191                 return VC_DB_ERROR_OPERATION_FAILED;
1192         }
1193         ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
1194         if (ret != SQLITE_OK) {
1195                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1196                 sqlite3_finalize(stmt);
1197                 return VC_DB_ERROR_OPERATION_FAILED;
1198         }
1199         ret = sqlite3_step(stmt);
1200         if (ret == SQLITE_DONE) {
1201                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1202                 sqlite3_clear_bindings(stmt);
1203                 sqlite3_finalize(stmt);
1204                 return VC_DB_ERROR_NONE;
1205         }
1206
1207         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
1208
1209         while (SQLITE_ROW == ret) {
1210                 int temp = 0;
1211                 vc_cmd_s* temp_cmd = NULL;
1212                 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
1213                 if (NULL == temp_cmd) {
1214                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allocation fail");
1215
1216                         if (NULL != temp_pid_list) {
1217                                 g_slist_free_full(temp_pid_list, free);
1218                                 temp_pid_list = NULL;
1219                         }
1220
1221                         sqlite3_reset(stmt);
1222                         sqlite3_clear_bindings(stmt);
1223                         sqlite3_finalize(stmt);
1224                         return VC_DB_ERROR_OUT_OF_MEMORY;
1225                 }
1226
1227                 temp = sqlite3_column_int(stmt, 0);
1228                 temp_cmd->pid = temp;
1229
1230                 temp = sqlite3_column_int(stmt, 1);
1231                 temp_cmd->type = temp;
1232
1233                 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
1234
1235                 ret = sqlite3_step(stmt);
1236                 if (SQLITE_DONE == ret)
1237                         break;
1238         }
1239
1240         *pid_list = temp_pid_list;
1241
1242         sqlite3_reset(stmt);
1243         sqlite3_clear_bindings(stmt);
1244         sqlite3_finalize(stmt);
1245         return VC_DB_ERROR_NONE;
1246 }
1247
1248 static int __vc_db_append_commands(sqlite3* db_handle, int pid, int type, vc_cmd_list_h vc_cmd_list)
1249 {
1250         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
1251
1252         int ret = 0;
1253         sqlite3_stmt* stmt = NULL;
1254         const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
1255
1256         /* check whether there is a table or not */
1257         bool is_exist = false;
1258         ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
1259         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1260                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
1261                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
1262                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1263                         return VC_DB_ERROR_OPERATION_FAILED;
1264                 }
1265         } else {
1266                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
1267         }
1268
1269         /* prepare db */
1270         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1271         if (ret != SQLITE_OK) {
1272                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1273                 return VC_DB_ERROR_OPERATION_FAILED;
1274         }
1275         ret = sqlite3_bind_int(stmt, 1, pid);
1276         if (ret != SQLITE_OK) {
1277                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1278                 sqlite3_finalize(stmt);
1279                 return VC_DB_ERROR_OPERATION_FAILED;
1280         }
1281         ret = sqlite3_bind_int(stmt, 2, type);
1282         if (ret != SQLITE_OK) {
1283                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1284                 sqlite3_clear_bindings(stmt);
1285                 sqlite3_finalize(stmt);
1286                 return VC_DB_ERROR_OPERATION_FAILED;
1287         }
1288         ret = sqlite3_step(stmt);
1289         if (ret == SQLITE_DONE) {
1290                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1291                 sqlite3_clear_bindings(stmt);
1292                 sqlite3_finalize(stmt);
1293                 return VC_DB_ERROR_NONE;
1294         }
1295
1296         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
1297
1298         vc_cmd_h temp_cmd = NULL;
1299
1300         while (SQLITE_ROW == ret) {
1301                 int temp = 0;
1302                 char* temp_text = NULL;
1303
1304                 if (0 != vc_cmd_create(&temp_cmd)) {
1305                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
1306                         sqlite3_reset(stmt);
1307                         sqlite3_clear_bindings(stmt);
1308                         sqlite3_finalize(stmt);
1309                         return -1;
1310                 }
1311                 temp = sqlite3_column_int(stmt, 0);
1312                 vc_cmd_set_id(temp_cmd, temp);
1313
1314                 temp = sqlite3_column_int(stmt, 1);
1315                 vc_cmd_set_pid(temp_cmd, temp);
1316
1317                 temp = sqlite3_column_int(stmt, 2);
1318                 vc_cmd_set_type(temp_cmd, temp);
1319
1320                 temp = sqlite3_column_int(stmt, 3);
1321                 vc_cmd_set_format(temp_cmd, temp);
1322
1323                 temp = sqlite3_column_int(stmt, 4);
1324                 vc_cmd_set_domain(temp_cmd, temp);
1325
1326                 temp_text = (char*)sqlite3_column_text(stmt, 5);
1327                 if (NULL != temp_text)
1328                         vc_cmd_set_command(temp_cmd, temp_text);
1329
1330                 temp_text = (char*)sqlite3_column_text(stmt, 6);
1331                 if (NULL != temp_text)
1332                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
1333
1334                 temp_text = (char*)sqlite3_column_text(stmt, 7);
1335                 if (NULL != temp_text)
1336                         vc_cmd_set_appid(temp_cmd, temp_text);
1337
1338                 temp_text = (char*)sqlite3_column_text(stmt, 8);
1339                 if (NULL != temp_text)
1340                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
1341
1342                 temp_text = (char*)sqlite3_column_text(stmt, 9);
1343                 if (NULL != temp_text)
1344                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
1345
1346                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1347                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1348                         vc_cmd_destroy(temp_cmd);
1349                         vc_cmd_list_destroy(vc_cmd_list, true);
1350                         vc_cmd_list = NULL;
1351                         sqlite3_reset(stmt);
1352                         sqlite3_clear_bindings(stmt);
1353                         sqlite3_finalize(stmt);
1354                         return VC_DB_ERROR_OPERATION_FAILED;
1355                 }
1356
1357                 ret = sqlite3_step(stmt);
1358                 if (SQLITE_DONE == ret)
1359                         break;
1360         }
1361
1362         sqlite3_reset(stmt);
1363         sqlite3_clear_bindings(stmt);
1364         sqlite3_finalize(stmt);
1365         return VC_DB_ERROR_NONE;
1366 }
1367
1368 static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
1369 {
1370         if (NULL == src_cmd) {
1371                 SLOG(LOG_WARN, vc_db_tag(), "[Client Data] Input command is NULL");
1372                 return NULL;
1373         }
1374
1375         vc_cmd_s* temp_cmd = NULL;
1376         temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
1377         if (NULL == temp_cmd) {
1378                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1379                 return NULL;
1380         }
1381
1382         temp_cmd->id = src_cmd->id;
1383         temp_cmd->pid = src_cmd->pid;
1384         temp_cmd->index = src_cmd->index;
1385         temp_cmd->type = src_cmd->type;
1386         temp_cmd->format = src_cmd->format;
1387         temp_cmd->domain = src_cmd->domain;
1388
1389         if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type)           temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
1390         else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
1391         else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
1392         else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1393         else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
1394         else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
1395
1396         if (NULL != src_cmd->command) {
1397                 temp_cmd->command = strdup(src_cmd->command);
1398         }
1399
1400         if (NULL != src_cmd->parameter) {
1401                 temp_cmd->parameter = strdup(src_cmd->parameter);
1402         }
1403
1404         if (NULL != src_cmd->appid) {
1405                 temp_cmd->appid = strdup(src_cmd->appid);
1406         }
1407
1408         if (NULL != src_cmd->invocation_name) {
1409                 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
1410         }
1411
1412         if (NULL != src_cmd->fixed) {
1413                 temp_cmd->fixed = strdup(src_cmd->fixed);
1414         }
1415
1416         temp_cmd->key = src_cmd->key;
1417         temp_cmd->modifier = src_cmd->modifier;
1418
1419         return temp_cmd;
1420 }
1421
1422 static int __vc_db_command_destroy(vc_cmd_h vc_command)
1423 {
1424         if (NULL == vc_command) {
1425                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Input parameter is NULL");
1426                 return VC_ERROR_INVALID_PARAMETER;
1427         }
1428
1429         vc_cmd_s* command = NULL;
1430         command = (vc_cmd_s*)vc_command;
1431
1432         SLOG(LOG_DEBUG, vc_db_tag(), "[Destroy command][%p]", command);
1433
1434         if (NULL != command) {
1435                 if (NULL != command->command) {
1436                         free(command->command);
1437                         command->command = NULL;
1438                 }
1439                 if (NULL != command->parameter) {
1440                         free(command->parameter);
1441                         command->parameter = NULL;
1442                 }
1443                 if (NULL != command->invocation_name) {
1444                         free(command->invocation_name);
1445                         command->invocation_name = NULL;
1446                 }
1447                 if (NULL != command->appid) {
1448                         free(command->appid);
1449                         command->appid = NULL;
1450                 }
1451                 if (NULL != command->fixed) {
1452                         free(command->fixed);
1453                         command->fixed = NULL;
1454                 }
1455                 if (NULL != command->coordinates) {
1456                         free(command->coordinates);
1457                         command->coordinates = NULL;
1458                 }
1459                 free(command);
1460                 command = NULL;
1461         }
1462
1463         return VC_ERROR_NONE;
1464 }
1465
1466
1467 static int __vc_db_create_table(sqlite3* db_handle, const char* table)
1468 {
1469         SLOG(LOG_INFO, vc_db_tag(), "[INFO] Create DB table (%s)", table);
1470         char* sql = NULL;
1471
1472         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1473                 sql = strdup("CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
1474                                 pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
1475         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1476                 sql = strdup("CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
1477                                 command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
1478         } else {
1479                 return VC_DB_ERROR_INVALID_PARAMETER;
1480         }
1481
1482         int ret = __vc_db_exec_query(db_handle, sql);
1483         if (ret != VC_DB_ERROR_NONE) {
1484                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", table, ret);
1485                 free(sql);
1486                 sql = NULL;
1487                 return VC_DB_ERROR_OPERATION_FAILED;
1488         }
1489         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1490
1491         free(sql);
1492         sql = NULL;
1493         return VC_DB_ERROR_NONE;
1494 }
1495
1496 int __vc_db_open_db_for_daemon(char** path, sqlite3** db_handle)
1497 {
1498         struct stat stat;
1499         int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1500         if (ret != SQLITE_OK) {
1501                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret(%d)", *path, ret);
1502                 if (db_handle && *db_handle) {
1503                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Error msg (%s)", sqlite3_errmsg(*db_handle));
1504                         db_util_close(*db_handle);
1505                         *db_handle = NULL;
1506                 }
1507
1508                 free(*path);
1509                 *path = NULL;
1510                 return VC_DB_ERROR_OPERATION_FAILED;
1511         }
1512
1513         if (lstat(*path, &stat) < 0) {
1514                 char buf_err[256];
1515                 SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
1516                 if (*db_handle)
1517                         db_util_close(*db_handle);
1518                 *db_handle = NULL;
1519
1520                 free(*path);
1521                 *path = NULL;
1522                 return VC_DB_ERROR_OPERATION_FAILED;
1523         }
1524
1525         if (!S_ISREG(stat.st_mode)) {
1526                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1527                 if (*db_handle)
1528                         db_util_close(*db_handle);
1529                 *db_handle = NULL;
1530
1531                 free(*path);
1532                 *path = NULL;
1533                 return VC_DB_ERROR_OPERATION_FAILED;
1534         }
1535
1536         if (!stat.st_size) {
1537                 __vc_db_begin_transaction(*db_handle);
1538
1539                 int ret = -1;
1540                 ret = __vc_db_create_table(*db_handle, VC_INFO_TABLE);
1541                 if (ret != VC_DB_ERROR_NONE) {
1542                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1543                         __vc_db_rollback_transaction(*db_handle);
1544                         return VC_DB_ERROR_OPERATION_FAILED;
1545                 }
1546                 ret = __vc_db_create_table(*db_handle, VC_RESULT_TABLE);
1547                 if (ret != VC_DB_ERROR_NONE) {
1548                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1549                         __vc_db_rollback_transaction(*db_handle);
1550                         return VC_DB_ERROR_OPERATION_FAILED;
1551                 }
1552
1553                 __vc_db_commit_transaction(*db_handle);
1554         }
1555
1556         if (*db_handle) {
1557                 char* err_msg = NULL;
1558                 static const char* sql = "PRAGMA journal_mode = WAL";
1559                 int ret = sqlite3_exec(*db_handle, sql, NULL, NULL, &err_msg);
1560                 if (ret != SQLITE_OK) {
1561                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1562                         sqlite3_free(err_msg);
1563                         err_msg = NULL;
1564                         return VC_DB_ERROR_OPERATION_FAILED;
1565                 }
1566         }
1567         return VC_DB_ERROR_NONE;
1568 }
1569
1570 static int __vc_db_restore_table(sqlite3* db_handle, const char* table)
1571 {
1572         /* check whether there is a vc_info table or not */
1573         bool is_exist = false;
1574         int ret = __vc_db_check_table(db_handle, table, &is_exist);
1575         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1576                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", table, ret);
1577                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, table)) {
1578                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", table, ret);
1579                         return VC_DB_ERROR_OPERATION_FAILED;
1580                 }
1581         } else {
1582                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", table);
1583         }
1584         return VC_DB_ERROR_NONE;
1585 }
1586
1587 bool __vc_db_connect_db_for_daemon(char** path, sqlite3** db_handle)
1588 {
1589         bool is_connect = false;
1590         int ret = __vc_db_open_db_for_daemon(path, db_handle);
1591         if (0 != ret) {
1592                 int cnt = 0;
1593                 while (cnt <= 5) {
1594                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB for daemon");
1595
1596                         usleep(100000);
1597                         if (0 == __vc_db_open_db_for_daemon(path, db_handle)) {
1598                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB for daemon");
1599                                 is_connect = true;
1600                                 break;
1601                         }
1602                         cnt++;
1603                 }
1604         } else {
1605                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB for daemon");
1606                 is_connect = true;
1607         }
1608         return is_connect;
1609 }
1610
1611 static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
1612 {
1613         SLOG(LOG_INFO, vc_db_tag(), "integrity check cb is called");
1614
1615         int ret;
1616         char *check_str = "ok";
1617         if (0 != strncmp(argv[0], check_str, strlen(check_str))) {
1618                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Doesn't have integrity(%s), retry to connect after removing", argv[0]);
1619                 if (0 != remove(g_path)) {
1620                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
1621                         g_db_cnt = (g_db_cnt + 1) % 1000;
1622                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1623                 }
1624                 bool is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
1625                 if (true == is_connect) {
1626                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
1627                         ret = __vc_db_restore_table(g_db_handle, VC_INFO_TABLE);
1628                         if (0 != ret) {
1629                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s)", VC_INFO_TABLE);
1630                         }
1631                         ret = __vc_db_restore_table(g_db_handle, VC_RESULT_TABLE);
1632                         if (0 != ret) {
1633                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s)", VC_RESULT_TABLE);
1634                         }
1635                         is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
1636                         if (true == is_connect) {
1637                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
1638                                 if (0 != vc_db_restore_command()) {
1639                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1640                                 }
1641                         }
1642                 } else {
1643                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
1644                         return VC_DB_ERROR_OPERATION_FAILED;
1645                 }
1646         }
1647
1648         SLOG(LOG_INFO, vc_db_tag(), "db integrity result : %s", argv[0]);
1649         return VC_DB_ERROR_NONE;
1650 }
1651
1652 int vc_db_initialize_for_daemon(void)
1653 {
1654         SLOG(LOG_INFO, vc_db_tag(), "DB on initialization for daemon");
1655
1656         if (0 < g_ref_cnt) {
1657                 g_ref_cnt++;
1658                 return VC_DB_ERROR_NONE;
1659         }
1660
1661         /* For voice control DB */
1662         g_path = (char*)calloc(256, sizeof(char));
1663         if (NULL == g_path) {
1664                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1665                 return VC_DB_ERROR_OUT_OF_MEMORY;
1666         }
1667         /* This should be changed to general DB space - TZ_USER_DB */
1668         snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1669
1670         /* For Backup DB */
1671         g_backup_path = (char*)calloc(256, sizeof(char));
1672         if (NULL == g_backup_path) {
1673                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1674                 return VC_DB_ERROR_OUT_OF_MEMORY;
1675         }
1676         snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
1677
1678         bool is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
1679         if (false == is_connect) {
1680                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing");
1681                 if (0 != remove(g_path)) {
1682                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
1683                         g_db_cnt = (g_db_cnt + 1) % 1000;
1684                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1685                 }
1686                 is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
1687                 if (true == is_connect) {
1688                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
1689                         is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
1690                         if (true == is_connect) {
1691                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
1692                                 if (0 != vc_db_restore_command()) {
1693                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1694                                 }
1695                         }
1696                 } else {
1697                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
1698                         return VC_DB_ERROR_OPERATION_FAILED;
1699                 }
1700         }
1701
1702         int ret = sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL);
1703         if (SQLITE_CORRUPT == ret) {
1704                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB for daemon");
1705
1706                 ret = db_util_close(g_db_handle);
1707                 if (ret != SQLITE_OK) {
1708                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to close db, ret %d: %s for daemon", ret, sqlite3_errmsg(g_db_handle));
1709                 }
1710                 g_db_handle = NULL;
1711
1712                 if (0 != remove(g_path)) {
1713                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
1714                         g_db_cnt = (g_db_cnt + 1) % 1000;
1715                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1716                 }
1717                 is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
1718                 if (true == is_connect) {
1719                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
1720                         is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
1721                         if (true == is_connect) {
1722                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
1723                                 if (0 != vc_db_restore_command()) {
1724                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command for daemon");
1725                                 }
1726                         }
1727                 } else {
1728                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
1729                         return VC_DB_ERROR_OPERATION_FAILED;
1730                 }
1731
1732                 g_ref_cnt++;
1733                 SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization after restore for daemon");
1734                 return VC_DB_ERROR_NONE;
1735         }
1736
1737         is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
1738         if (false == is_connect) {
1739                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file for daemon");
1740                 if (0 != remove(g_backup_path)) {
1741                         g_backup_db_cnt = (g_backup_db_cnt + 1) % 1000;
1742                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
1743                         snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
1744                 }
1745                 is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_backup_handle);
1746                 if (true == is_connect) {
1747                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
1748                         if (0 != vc_db_restore_command()) {
1749                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command for daemon");
1750                         }
1751                 }
1752         }
1753
1754         g_ref_cnt++;
1755
1756         SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization for daemon");
1757         return VC_DB_ERROR_NONE;
1758 }
1759
1760 int __vc_db_open_db(char** path, sqlite3** db_handle)
1761 {
1762         int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1763         if (ret != SQLITE_OK) {
1764                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db for daemon, path = %s, ret(%d)", *path, ret);
1765                 if (db_handle && *db_handle) {
1766                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Error msg (%s)", sqlite3_errmsg(*db_handle));
1767                         db_util_close(*db_handle);
1768                         *db_handle = NULL;
1769                 }
1770
1771                 free(*path);
1772                 *path = NULL;
1773                 return VC_DB_ERROR_OPERATION_FAILED;
1774         }
1775         return VC_DB_ERROR_NONE;
1776 }
1777
1778 bool __vc_db_connect_db(char** path, sqlite3** db_handle)
1779 {
1780         bool is_connect = false;
1781         int ret = __vc_db_open_db(path, db_handle);
1782         if (0 != ret) {
1783                 int cnt = 0;
1784                 while (cnt <= 5) {
1785                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1786
1787                         usleep(100000);
1788                         if (0 == __vc_db_open_db(path, db_handle)) {
1789                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
1790                                 is_connect = true;
1791                                 break;
1792                         }
1793                         cnt++;
1794                 }
1795         } else {
1796                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
1797                 is_connect = true;
1798         }
1799         return is_connect;
1800 }
1801
1802 int vc_db_initialize(void)
1803 {
1804         SLOG(LOG_INFO, vc_db_tag(), "DB on initialization");
1805
1806         if (0 < g_ref_cnt) {
1807                 g_ref_cnt++;
1808                 return VC_DB_ERROR_NONE;
1809         }
1810
1811         /* For voice control DB */
1812         g_path = (char*)calloc(256, sizeof(char));
1813         if (NULL == g_path) {
1814                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1815                 return VC_DB_ERROR_OUT_OF_MEMORY;
1816         }
1817         /* This should be changed to general DB space - TZ_USER_DB */
1818         snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1819
1820         /* For Backup DB */
1821         g_backup_path = (char*)calloc(256, sizeof(char));
1822         if (NULL == g_backup_path) {
1823                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1824                 return VC_DB_ERROR_OUT_OF_MEMORY;
1825         }
1826         snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
1827
1828         bool is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1829         if (false == is_connect) {
1830                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing");
1831                 if (0 != remove(g_path)) {
1832                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1833                         g_db_cnt = (g_db_cnt + 1) % 1000;
1834                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1835                 }
1836                 is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1837                 if (true == is_connect) {
1838                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
1839                         is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1840                         if (true == is_connect) {
1841                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
1842                                 if (0 != vc_db_restore_command()) {
1843                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1844                                 }
1845                         }
1846                 } else {
1847                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
1848                         return VC_DB_ERROR_OPERATION_FAILED;
1849                 }
1850         }
1851
1852         is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1853         if (false == is_connect) {
1854                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file");
1855                 if (0 != remove(g_backup_path)) {
1856                         g_backup_db_cnt = (g_backup_db_cnt + 1) % 1000;
1857                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
1858                         snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
1859                 }
1860                 is_connect = __vc_db_connect_db(&g_path, &g_db_backup_handle);
1861                 if (true == is_connect) {
1862                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup");
1863                         if (0 != vc_db_restore_command()) {
1864                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1865                         }
1866                 }
1867         }
1868
1869         g_ref_cnt++;
1870
1871         SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization");
1872         return VC_DB_ERROR_NONE;
1873 }
1874
1875 int vc_db_finalize(void)
1876 {
1877         if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
1878         if (0 != --g_ref_cnt)
1879                 return VC_DB_ERROR_NONE;
1880
1881         if (NULL != g_path) {
1882                 free(g_path);
1883                 g_path = NULL;
1884         }
1885
1886         if (NULL != g_backup_path) {
1887                 free(g_backup_path);
1888                 g_backup_path = NULL;
1889         }
1890
1891         if (!g_db_handle)
1892                 return VC_DB_ERROR_NONE;
1893         db_util_close(g_db_handle);
1894         g_db_handle = NULL;
1895
1896         if (!g_db_backup_handle)
1897                 return VC_DB_ERROR_NONE;
1898         db_util_close(g_db_backup_handle);
1899         g_db_backup_handle = NULL;
1900
1901         return VC_DB_ERROR_NONE;
1902 }
1903
1904 int vc_db_create_table()
1905 {
1906         __vc_db_reset_handle();
1907         __vc_db_begin_transaction(g_db_handle);
1908
1909         int ret = __vc_db_create_table(g_db_handle, VC_INFO_TABLE);
1910         if (ret != VC_DB_ERROR_NONE) {
1911                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1912                 __vc_db_rollback_transaction(g_db_handle);
1913                 return VC_DB_ERROR_OPERATION_FAILED;
1914         }
1915         ret = __vc_db_create_table(g_db_handle, VC_RESULT_TABLE);
1916         if (ret != VC_DB_ERROR_NONE) {
1917                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1918                 __vc_db_rollback_transaction(g_db_handle);
1919                 return VC_DB_ERROR_OPERATION_FAILED;
1920         }
1921
1922         __vc_db_commit_transaction(g_db_handle);
1923         return VC_DB_ERROR_NONE;
1924 }
1925
1926 int __vc_db_delete_table(sqlite3* db_handle, const char* table)
1927 {
1928         char* sql = NULL;
1929         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1930                 sql = strdup("DELETE FROM vc_result;");
1931         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1932                 sql = strdup("DELETE FROM vc_info;");
1933         } else {
1934                 return VC_DB_ERROR_INVALID_PARAMETER;
1935         }
1936
1937         if (NULL == sql) {
1938                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1939                 return VC_DB_ERROR_OUT_OF_MEMORY;
1940         }
1941
1942         int ret = __vc_db_exec_query(db_handle, sql);
1943         if (ret != VC_DB_ERROR_NONE) {
1944                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1945                 free(sql);
1946                 sql = NULL;
1947                 return ret;
1948         }
1949
1950         free(sql);
1951         sql = NULL;
1952
1953         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1954                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1955         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1956                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1957         }
1958
1959         if (NULL == sql) {
1960                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1961                 return VC_DB_ERROR_OUT_OF_MEMORY;
1962         }
1963
1964         ret = __vc_db_exec_query(db_handle, sql);
1965         if (ret != VC_DB_ERROR_NONE) {
1966                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1967                 free(sql);
1968                 sql = NULL;
1969                 return ret;
1970         }
1971
1972         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1973
1974         free(sql);
1975         sql = NULL;
1976         return VC_DB_ERROR_NONE;
1977 }
1978
1979 int vc_db_delete_table(const char* table)
1980 {
1981         __vc_db_reset_handle();
1982         __vc_db_begin_transaction(g_db_handle);
1983
1984         int ret = __vc_db_delete_table(g_db_handle, table);
1985         if (0 != ret) {
1986                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1987                 __vc_db_rollback_transaction(g_db_handle);
1988                 return ret;
1989         }
1990
1991         __vc_db_commit_transaction(g_db_handle);
1992
1993         return VC_DB_ERROR_NONE;
1994 }
1995
1996 int vc_db_begin_transaction(void)
1997 {
1998         __vc_db_reset_handle();
1999         int ret = __vc_db_begin_transaction(g_db_handle);
2000         return ret;
2001 }
2002
2003 int vc_db_rollback_transaction(void)
2004 {
2005         __vc_db_reset_handle();
2006         int ret = __vc_db_rollback_transaction(g_db_handle);
2007         return ret;
2008 }
2009
2010 int vc_db_commit_transaction(void)
2011 {
2012         __vc_db_reset_handle();
2013         int ret = __vc_db_commit_transaction(g_db_handle);
2014         return ret;
2015 }
2016
2017 static void __vc_db_remove_space(char** string)
2018 {
2019         if (NULL == string || NULL == *string)
2020                 return;
2021
2022         char* temp = *string;
2023
2024         //remove previous space
2025         if (' ' == temp[0])
2026                 memmove(temp, temp + 1, strlen(temp));
2027
2028         // remove next space
2029         if (' ' == temp[strlen(temp) - 1])
2030                 temp[strlen(temp) - 1] = '\0';
2031 }
2032
2033 static bool __vc_db_is_valid_vfixed_string(char* string)
2034 {
2035         char* temp = strchr(string, '}');
2036         if (NULL == temp)
2037                 return false;
2038
2039         temp = strchr(string, '{');
2040         if (NULL == temp)
2041                 return false;
2042
2043         temp = strchr(string, '|');
2044         if (NULL == temp)
2045                 return false;
2046         return true;
2047 }
2048
2049 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
2050 {
2051         if (NULL == cmd || NULL == cmd->command) {
2052                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
2053                 return VC_DB_ERROR_INVALID_PARAMETER;
2054         }
2055
2056         GSList* temp_list = NULL;
2057         char* temp = NULL;
2058         char* src_cmd = strdup(cmd->command);
2059         char* dst_cmd = NULL;
2060         char merge_cmd[256] = {0, };
2061
2062         if (NULL == src_cmd) {
2063                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
2064                 return VC_DB_ERROR_OUT_OF_MEMORY;
2065         }
2066
2067         if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
2068                 // check string validation
2069                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
2070                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
2071                         free(src_cmd);
2072                         src_cmd = NULL;
2073                         return VC_DB_ERROR_INVALID_PARAMETER;
2074                 }
2075
2076                 // remove close brace, '}'
2077                 char* temp_close = strchr(src_cmd, '}');
2078                 if (NULL != temp_close) {
2079                         temp_close[0] = '\0';
2080                 }
2081
2082                 // extract fixed command and remove space in front of  '{'
2083                 char *tok_ptr = NULL;
2084                 temp = strtok_r(src_cmd, "{", &tok_ptr);
2085                 if (NULL != temp) {
2086                         __vc_db_remove_space(&temp);
2087                         *fixed_cmd = strdup(temp);
2088
2089                         // merge command with fixed and vfixed
2090                         while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
2091                                 __vc_db_remove_space(&temp);
2092
2093                                 snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
2094                                 dst_cmd = strdup(merge_cmd);
2095                                 temp_list = g_slist_append(temp_list, dst_cmd);
2096                                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
2097                         }
2098                 } else {
2099                         *fixed_cmd = strdup(cmd->command);
2100                 }
2101         } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
2102                 // check string validation
2103                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
2104                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
2105                         free(src_cmd);
2106                         src_cmd = NULL;
2107                         return VC_DB_ERROR_INVALID_PARAMETER;
2108                 }
2109
2110                 // extract fixed command
2111                 char* temp_fixed = strchr(src_cmd, '}') + 1;
2112                 __vc_db_remove_space(&temp_fixed);
2113                 *fixed_cmd = strdup(temp_fixed);
2114
2115                 // remove close brace, '}'
2116                 char *tok_ptr = NULL;
2117                 temp = strtok_r(src_cmd, "}", &tok_ptr);
2118
2119                 // remove open brace, '{'
2120                 temp = strchr(src_cmd, '{') + 1;
2121
2122                 tok_ptr = NULL;
2123                 temp = strtok_r(temp, "|", &tok_ptr);
2124                 __vc_db_remove_space(&temp);
2125
2126                 // merge command with fixed and vfixed
2127                 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
2128                 dst_cmd = strdup(merge_cmd);
2129                 temp_list = g_slist_append(temp_list, dst_cmd);
2130                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
2131
2132                 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
2133                         __vc_db_remove_space(&temp);
2134
2135                         // merge command with fixed and vfixed
2136                         snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
2137                         dst_cmd = strdup(merge_cmd);
2138                         temp_list = g_slist_append(temp_list, dst_cmd);
2139                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
2140                 }
2141         } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
2142                 dst_cmd = strdup(src_cmd);
2143                 temp_list = g_slist_append(temp_list, dst_cmd);
2144                 *fixed_cmd = strdup(src_cmd);
2145
2146         } else {
2147                 dst_cmd = strdup(src_cmd);
2148                 temp_list = g_slist_append(temp_list, dst_cmd);
2149         }
2150
2151         *cmd_list = temp_list;
2152
2153         free(src_cmd);
2154         src_cmd = NULL;
2155         return VC_DB_ERROR_NONE;
2156 }
2157
2158 static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
2159 {
2160         GSList* cmd_list = NULL;
2161         char* fixed_cmd = NULL;
2162         vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
2163
2164         int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
2165         if (VC_DB_ERROR_NONE != ret) {
2166                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
2167
2168                 if (NULL != tmp_cmd) {
2169                         __vc_db_command_destroy((vc_cmd_h)tmp_cmd);
2170                 }
2171                 return ret;
2172         }
2173
2174         if (0 != g_slist_length(cmd_list)) {
2175                 GSList *iter = NULL;
2176                 char* temp_command = NULL;
2177                 iter = g_slist_nth(cmd_list, 0);
2178
2179                 while (NULL != iter) {
2180                         temp_command = iter->data;
2181
2182                         if (NULL != temp_command) {
2183                                 if (NULL != tmp_cmd->command) {
2184                                         free(tmp_cmd->command);
2185                                         tmp_cmd->command = NULL;
2186                                 }
2187                                 tmp_cmd->command = strdup(temp_command);
2188
2189                                 free(tmp_cmd->fixed);
2190                                 if (NULL != fixed_cmd)
2191                                         tmp_cmd->fixed = strdup(fixed_cmd);
2192                                 else
2193                                         tmp_cmd->fixed = NULL;
2194
2195                                 ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
2196                                 if (ret != VC_DB_ERROR_NONE) {
2197                                         break;
2198                                 }
2199
2200                                 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name && false == skip_invocation) {
2201                                         char temp[256] = {0, };
2202                                         snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
2203                                         if (NULL != tmp_cmd->command)
2204                                                 free(tmp_cmd->command);
2205
2206                                         tmp_cmd->command = strdup(temp);
2207
2208                                         ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
2209                                         if (ret != VC_DB_ERROR_NONE) {
2210                                                 break;
2211                                         }
2212                                 }
2213                                 cmd_list = g_slist_remove(cmd_list, temp_command);
2214                                 free(temp_command);
2215                                 temp_command = NULL;
2216                         }
2217                         iter = g_slist_nth(cmd_list, 0);
2218                 }
2219
2220                 if (VC_DB_ERROR_NONE != ret) {
2221                         while (NULL != iter) {
2222                                 temp_command = iter->data;
2223
2224                                 if (NULL != temp_command) {
2225                                         cmd_list = g_slist_remove(cmd_list, temp_command);
2226                                         free(temp_command);
2227                                         temp_command = NULL;
2228                                 }
2229
2230                                 iter = g_slist_nth(cmd_list, 0);
2231                         }
2232
2233                         g_slist_free(cmd_list);
2234                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
2235                 }
2236
2237                 cmd_list = NULL;
2238         }
2239
2240         if (NULL != fixed_cmd) {
2241                 free(fixed_cmd);
2242                 fixed_cmd = NULL;
2243         }
2244
2245         if (NULL != tmp_cmd) {
2246                 __vc_db_command_destroy((vc_cmd_h)tmp_cmd);
2247                 tmp_cmd = NULL;
2248                 SLOG(LOG_DEBUG, vc_db_tag(), "[DEBUG] command destroy");
2249         }
2250
2251         return ret;
2252 }
2253
2254 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
2255 {
2256         int cnt = 0;
2257         int ret = -1;
2258         while (cnt < 5 && (ret = __vc_db_insert_command(g_db_handle, pid, type, cmd, skip_invocation))) {
2259                 usleep(100000);
2260                 SLOG(LOG_DEBUG, vc_db_tag(), "[ERROR] Fail to insert command(%d), retry cnt(%d)", ret, cnt);
2261                 cnt++;
2262         }
2263         return ret;
2264 }
2265
2266 static int __vc_db_insert_commands_list(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
2267 {
2268         GSList *iter = NULL;
2269         vc_cmd_s *temp_cmd;
2270
2271         int i;
2272         int count = g_slist_length(cmd_list);
2273         iter = g_slist_nth(cmd_list, 0);
2274
2275         SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
2276
2277         for (i = 0; i < count; i++) {
2278                 if (NULL == iter)
2279                         break;
2280
2281                 temp_cmd = iter->data;
2282
2283                 if (NULL == temp_cmd) {
2284                         SLOG(LOG_ERROR, vc_db_tag(), "command is NULL");
2285                         break;
2286                 }
2287
2288                 if (type == temp_cmd->type) {
2289                         int ret = vc_cmd_set_invocation_name((vc_cmd_h)temp_cmd, invocation_name);
2290                         SLOG(LOG_DEBUG, vc_db_tag(), "Set invocation to command, ret(%d)", ret);
2291
2292                         ret = __vc_db_insert_command(db_handle, pid, type, temp_cmd, skip_invocation);
2293                         if (ret != VC_DB_ERROR_NONE) {
2294                                 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
2295                                 return ret;
2296                         }
2297                 } else {
2298                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
2299                 }
2300                 iter = g_slist_next(iter);
2301         }
2302
2303         return VC_DB_ERROR_NONE;
2304 }
2305
2306 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
2307 {
2308         __vc_db_reset_handle();
2309         __vc_db_begin_transaction(g_db_handle);
2310
2311         int ret = __vc_db_insert_commands_list(g_db_handle, pid, type, cmd_list, invocation_name, false);
2312         if (ret != VC_DB_ERROR_NONE) {
2313                 __vc_db_rollback_transaction(g_db_handle);
2314                 return ret;
2315         }
2316
2317         __vc_db_commit_transaction(g_db_handle);
2318
2319         return VC_DB_ERROR_NONE;
2320 }
2321
2322 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
2323 {
2324         __vc_db_reset_handle();
2325         __vc_db_begin_transaction(g_db_handle);
2326
2327         int ret = __vc_db_get_commands(g_db_handle, pid, type, cmd_list);
2328         if (ret != VC_DB_ERROR_NONE) {
2329                 __vc_db_rollback_transaction(g_db_handle);
2330                 return ret;
2331         }
2332
2333         __vc_db_commit_transaction(g_db_handle);
2334         return VC_DB_ERROR_NONE;
2335 }
2336
2337 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
2338 {
2339         if (NULL == result_text) {
2340                 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
2341                 return VC_DB_ERROR_INVALID_PARAMETER;
2342         }
2343
2344         int ret = vc_db_delete_table(VC_RESULT_TABLE);
2345         if (0 != ret)
2346                 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2347
2348         if (NULL == vc_cmd_list) {
2349                 __vc_db_reset_handle();
2350                 __vc_db_begin_transaction(g_db_handle);
2351                 int ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, NULL);
2352                 if (ret != VC_DB_ERROR_NONE) {
2353                         __vc_db_rollback_transaction(g_db_handle);
2354                         return ret;
2355                 }
2356                 __vc_db_commit_transaction(g_db_handle);
2357                 return VC_DB_ERROR_NONE;
2358         }
2359
2360         /* Make client list node */
2361         vc_cmd_h vc_command = NULL;
2362         vc_cmd_list_first(vc_cmd_list);
2363
2364         while (VC_ERROR_ITERATION_END != ret) {
2365                 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
2366                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
2367                         break;
2368                 }
2369
2370                 if (NULL == vc_command) {
2371                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
2372                         break;
2373                 }
2374
2375                 vc_cmd_s* temp_cmd = NULL;
2376                 temp_cmd = (vc_cmd_s*)vc_command;
2377
2378                 __vc_db_reset_handle();
2379                 __vc_db_begin_transaction(g_db_handle);
2380                 ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, temp_cmd);
2381                 if (ret != VC_DB_ERROR_NONE) {
2382                         __vc_db_rollback_transaction(g_db_handle);
2383                         return ret;
2384                 }
2385                 __vc_db_commit_transaction(g_db_handle);
2386
2387                 ret = vc_cmd_list_next(vc_cmd_list);
2388         }
2389
2390         return VC_DB_ERROR_NONE;
2391 }
2392
2393 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
2394 {
2395         __vc_db_reset_handle();
2396         __vc_db_begin_transaction(g_db_handle);
2397
2398         int ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
2399         if (ret != VC_DB_ERROR_NONE) {
2400                 __vc_db_rollback_transaction(g_db_handle);
2401                 return VC_DB_ERROR_OPERATION_FAILED;
2402         }
2403
2404         if (NULL == msg) {
2405                 int count = 0;
2406                 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
2407                 if (ret != VC_DB_ERROR_NONE) {
2408                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
2409                 }
2410                 if (0 == count) {
2411                         char* appid = NULL;
2412                         // Get appid by pid using app control
2413                         ret = app_manager_get_app_id(pid, &appid);
2414                         if (APP_MANAGER_ERROR_NONE != ret) {
2415                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
2416                         }
2417                         ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
2418                         if (ret != VC_DB_ERROR_NONE) {
2419                                 __vc_db_rollback_transaction(g_db_handle);
2420                                 return ret;
2421                         }
2422                         if (NULL != appid) {
2423                                 free(appid);
2424                                 appid = NULL;
2425                         }
2426                 }
2427         }
2428         __vc_db_commit_transaction(g_db_handle);
2429         return VC_DB_ERROR_NONE;
2430 }
2431
2432 int vc_db_get_appid_list(const char* result, GSList** app_list)
2433 {
2434         __vc_db_reset_handle();
2435         __vc_db_begin_transaction(g_db_handle);
2436
2437         int ret = __vc_db_get_appid(g_db_handle, result, app_list);
2438         if (ret != VC_DB_ERROR_NONE) {
2439                 __vc_db_rollback_transaction(g_db_handle);
2440                 return ret;
2441         }
2442
2443         __vc_db_commit_transaction(g_db_handle);
2444         return VC_DB_ERROR_NONE;
2445 }
2446
2447 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
2448 {
2449         __vc_db_reset_handle();
2450         __vc_db_begin_transaction(g_db_handle);
2451
2452         int ret = __vc_db_get_result_pid_list(g_db_handle, result, pid_list);
2453         if (ret != VC_DB_ERROR_NONE) {
2454                 __vc_db_rollback_transaction(g_db_handle);
2455                 return ret;
2456         }
2457
2458         __vc_db_commit_transaction(g_db_handle);
2459         return VC_DB_ERROR_NONE;
2460 }
2461
2462 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
2463 {
2464         __vc_db_reset_handle();
2465         __vc_db_begin_transaction(g_db_handle);
2466
2467         int ret = __vc_db_append_commands(g_db_handle, pid, type, vc_cmd_list);
2468         if (ret != VC_DB_ERROR_NONE) {
2469                 __vc_db_rollback_transaction(g_db_handle);
2470                 return ret;
2471         }
2472
2473         __vc_db_commit_transaction(g_db_handle);
2474         return VC_DB_ERROR_NONE;
2475 }
2476
2477 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
2478 {
2479         __vc_db_reset_handle();
2480         __vc_db_begin_transaction(g_db_handle);
2481
2482         int ret = 0;
2483         ret = __vc_db_delete_commands(g_db_handle, pid, type, appid);
2484         if (ret != VC_DB_ERROR_NONE) {
2485                 __vc_db_rollback_transaction(g_db_handle);
2486                 return ret;
2487         }
2488
2489         __vc_db_commit_transaction(g_db_handle);
2490         return VC_DB_ERROR_NONE;
2491 }
2492
2493 static void __free_command_list_element(gpointer data)
2494 {
2495         vc_cmd_h temp_cmd = (vc_cmd_h)data;
2496         int ret = vc_cmd_destroy(temp_cmd);
2497         if (ret != VC_ERROR_NONE) {
2498                 SLOG(LOG_ERROR, vc_db_tag(), "Fail to destroy command. (%d/%s)", ret, get_error_message(ret));
2499         }
2500 }
2501
2502 int vc_db_backup_command()
2503 {
2504         GSList* list = NULL;
2505
2506         __vc_db_reset_handle();
2507         __vc_db_begin_transaction(g_db_backup_handle);
2508
2509         int ret = __vc_db_delete_table(g_db_backup_handle, VC_INFO_TABLE);
2510         if (0 != ret) {
2511                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2512                 __vc_db_rollback_transaction(g_db_backup_handle);
2513                 return ret;
2514         }
2515         __vc_db_commit_transaction(g_db_backup_handle);
2516
2517         ret = vc_db_get_commands(-1, VC_COMMAND_TYPE_BACKGROUND, &list);
2518         if (ret != VC_DB_ERROR_NONE) {
2519                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands");
2520                 return ret;
2521         }
2522
2523         __vc_db_begin_transaction(g_db_backup_handle);
2524
2525         ret = __vc_db_insert_commands_list(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2526         if (ret != VC_DB_ERROR_NONE) {
2527                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list to backup db");
2528                 __vc_db_rollback_transaction(g_db_backup_handle);
2529         } else {
2530                 SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
2531                 __vc_db_commit_transaction(g_db_backup_handle);
2532         }
2533
2534         g_slist_free_full(list, __free_command_list_element);
2535         return ret;
2536 }
2537
2538 int vc_db_restore_command()
2539 {
2540         GSList* list = NULL;
2541
2542         __vc_db_reset_handle();
2543         __vc_db_begin_transaction(g_db_backup_handle);
2544
2545         int ret = __vc_db_get_commands(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, &list);
2546         if (ret != VC_DB_ERROR_NONE) {
2547                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands from backup db");
2548                 __vc_db_rollback_transaction(g_db_backup_handle);
2549                 return ret;
2550         }
2551         __vc_db_commit_transaction(g_db_backup_handle);
2552
2553         ret = vc_db_insert_commands_list(-1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2554         if (ret != VC_DB_ERROR_NONE) {
2555                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list. (%d)", ret);
2556         } else {
2557                 SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
2558         }
2559
2560         g_slist_free_full(list, __free_command_list_element);
2561         return ret;
2562 }