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