Fix issues that detected by static analysis tool
[platform/core/uifw/voice-control.git] / common / vc_cmd_db.c
1 /*
2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <app_manager.h>
18 #include <db-util.h>
19 #include <dlog.h>
20 #include <sqlite3.h>
21 #include <tzplatform_config.h>
22
23 #include "vc_cmd_db.h"
24 #include "vc_main.h"
25 #include "vc_command.h"
26 #include "voice_control_command_expand.h"
27
28
29 const char* vc_db_tag()
30 {
31         return TAG_VCDB;
32 }
33
34 /*!
35  * \note
36  * DB Table schema
37  *
38  * vc_info
39  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
40  * | id  | pid  | type | format | domain | command | parameter | appid | invocation_name | fixed |
41  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
42  *
43  * CREATE TABLE vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, appid TEXT);
44  *
45  * vc_result
46  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
47  * | id  | result | event | msg | exclusive | pid | type | format | domain | command | parameter | appid | invocation_name | fixed |
48  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
49  *
50  * CREATE TABLE vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,
51  *                                      pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT,\
52  *
53  */
54
55 #define FREE_MEM(X) {if (NULL != X) {free(X); X = NULL; } }
56
57
58 //#define DB_PATH tzplatform_mkpath(TZ_USER_DB, ".vc_info.db")
59 static sqlite3* g_db_handle = NULL;
60 static sqlite3* g_db_backup_handle = NULL;
61 static char* g_path = NULL;
62 static char* g_backup_path = NULL;
63 static int g_ref_cnt = 0;
64
65 int g_fpid = -1;
66 int g_db_cnt = 0;
67 int g_backup_db_cnt = 0;
68
69 static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist);
70 static int __vc_db_begin_transaction(sqlite3* db_handle);
71 static int __vc_db_create_table(sqlite3* db_handle, const char* table);
72 static int __vc_db_rollback_transaction(sqlite3* db_handle);
73 static int __vc_db_commit_transaction(sqlite3* db_handle);
74
75
76 int __vc_db_reset_handle(void)
77 {
78         if (!g_db_handle || !g_db_backup_handle) {
79                 vc_db_finalize();
80                 vc_db_initialize();
81         }
82         return 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                 free(sql);
1438                 sql = NULL;
1439                 return VC_DB_ERROR_OPERATION_FAILED;
1440         }
1441         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1442
1443         free(sql);
1444         sql = NULL;
1445         return VC_DB_ERROR_NONE;
1446 }
1447
1448 int __vc_db_open_db_for_daemon(char** path, sqlite3** db_handle)
1449 {
1450         struct stat stat;
1451         int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1452         if (ret != SQLITE_OK) {
1453                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", *path, ret, sqlite3_errmsg(*db_handle));
1454                 if (*db_handle) {
1455                         db_util_close(*db_handle);
1456                         *db_handle = NULL;
1457                 }
1458
1459                 free(*path);
1460                 *path = NULL;
1461                 return VC_DB_ERROR_OPERATION_FAILED;
1462         }
1463
1464         if (lstat(*path, &stat) < 0) {
1465                 char buf_err[256];
1466                 SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
1467                 if (*db_handle)
1468                         db_util_close(*db_handle);
1469                 *db_handle = NULL;
1470
1471                 free(*path);
1472                 *path = NULL;
1473                 return VC_DB_ERROR_OPERATION_FAILED;
1474         }
1475
1476         if (!S_ISREG(stat.st_mode)) {
1477                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1478                 if (*db_handle)
1479                         db_util_close(*db_handle);
1480                 *db_handle = NULL;
1481
1482                 free(*path);
1483                 *path = NULL;
1484                 return VC_DB_ERROR_OPERATION_FAILED;
1485         }
1486
1487         if (!stat.st_size) {
1488                 __vc_db_begin_transaction(*db_handle);
1489
1490                 int ret = -1;
1491                 ret = __vc_db_create_table(*db_handle, VC_INFO_TABLE);
1492                 if (ret != VC_DB_ERROR_NONE) {
1493                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1494                         __vc_db_rollback_transaction(*db_handle);
1495                         return VC_DB_ERROR_OPERATION_FAILED;
1496                 }
1497                 ret = __vc_db_create_table(*db_handle, VC_RESULT_TABLE);
1498                 if (ret != VC_DB_ERROR_NONE) {
1499                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1500                         __vc_db_rollback_transaction(*db_handle);
1501                         return VC_DB_ERROR_OPERATION_FAILED;
1502                 }
1503
1504                 __vc_db_commit_transaction(*db_handle);
1505         }
1506
1507         if (*db_handle) {
1508                 char* err_msg = NULL;
1509                 static const const char* sql = "PRAGMA journal_mode = WAL";
1510                 int ret = sqlite3_exec(*db_handle, sql, NULL, NULL, &err_msg);
1511                 if (ret != SQLITE_OK) {
1512                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1513                         return VC_DB_ERROR_OPERATION_FAILED;
1514                 }
1515         }
1516         return VC_DB_ERROR_NONE;
1517 }
1518
1519 static int __vc_db_restore_table(sqlite3* db_handle, const char* table)
1520 {
1521         /* check whether there is a vc_info table or not */
1522         bool is_exist = false;
1523         int ret = __vc_db_check_table(db_handle, table, &is_exist);
1524         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1525                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", table, ret);
1526                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, table)) {
1527                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", table, ret);
1528                         return VC_DB_ERROR_OPERATION_FAILED;
1529                 }
1530         } else {
1531                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", table);
1532         }
1533         return VC_DB_ERROR_NONE;
1534 }
1535
1536 bool __vc_db_connect_db_for_daemon(char** path, sqlite3** db_handle)
1537 {
1538         bool is_connect = false;
1539         int ret = __vc_db_open_db_for_daemon(path, db_handle);
1540         if (0 != ret) {
1541                 int cnt = 0;
1542                 while (cnt <= 5) {
1543                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB for daemon");
1544
1545                         usleep(100000);
1546                         if (0 == __vc_db_open_db_for_daemon(path, db_handle)) {
1547                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB for daemon");
1548                                 is_connect = true;
1549                                 break;
1550                         }
1551                         cnt++;
1552                 }
1553         } else {
1554                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB for daemon");
1555                 is_connect = true;
1556         }
1557         return is_connect;
1558 }
1559
1560 static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
1561 {
1562         SLOG(LOG_INFO, vc_db_tag(), "integrity check cb is called");
1563
1564         int ret;
1565         char *check_str = "ok";
1566         if (0 != strncmp(argv[0], check_str, strlen(check_str))) {
1567                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Doesn't have integrity(%s), retry to connect after removing", argv[0]);
1568                 if (0 != remove(g_path)) {
1569                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
1570                         g_db_cnt = (g_db_cnt + 1) % 1000;
1571                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1572                 }
1573                 bool is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
1574                 if (true == is_connect) {
1575                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
1576                         ret = __vc_db_restore_table(g_db_handle, VC_INFO_TABLE);
1577                         if (0 != ret) {
1578                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s)", VC_INFO_TABLE);
1579                         }
1580                         ret = __vc_db_restore_table(g_db_handle, VC_RESULT_TABLE);
1581                         if (0 != ret) {
1582                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s)", VC_RESULT_TABLE);
1583                         }
1584                         is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
1585                         if (true == is_connect) {
1586                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
1587                                 if (0 != vc_db_restore_command()) {
1588                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1589                                 }
1590                         }
1591                 } else {
1592                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
1593                         return VC_DB_ERROR_OPERATION_FAILED;
1594                 }
1595         }
1596
1597         SLOG(LOG_INFO, vc_db_tag(), "db integrity result : %s", argv[0]);
1598         return VC_DB_ERROR_NONE;
1599 }
1600
1601 int vc_db_initialize_for_daemon(void)
1602 {
1603         SLOG(LOG_INFO, vc_db_tag(), "DB on initialization for daemon");
1604
1605         if (0 < g_ref_cnt) {
1606                 g_ref_cnt++;
1607                 return VC_DB_ERROR_NONE;
1608         }
1609
1610         /* For voice control DB */
1611         g_path = (char*)calloc(256, sizeof(char));
1612         if (NULL == g_path) {
1613                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1614                 return VC_DB_ERROR_OUT_OF_MEMORY;
1615         }
1616         /* This should be changed to general DB space - TZ_USER_DB */
1617         snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1618
1619         /* For Backup DB */
1620         g_backup_path = (char*)calloc(256, sizeof(char));
1621         if (NULL == g_backup_path) {
1622                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1623                 return VC_DB_ERROR_OUT_OF_MEMORY;
1624         }
1625         snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
1626
1627         bool is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
1628         if (false == is_connect) {
1629                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing");
1630                 if (0 != remove(g_path)) {
1631                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
1632                         g_db_cnt = (g_db_cnt + 1) % 1000;
1633                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1634                 }
1635                 is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
1636                 if (true == is_connect) {
1637                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
1638                         is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
1639                         if (true == is_connect) {
1640                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
1641                                 if (0 != vc_db_restore_command()) {
1642                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1643                                 }
1644                         }
1645                 } else {
1646                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
1647                         return VC_DB_ERROR_OPERATION_FAILED;
1648                 }
1649         }
1650
1651         int ret = sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL);
1652         if (SQLITE_CORRUPT == ret) {
1653                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB for daemon");
1654
1655                 ret = db_util_close(g_db_handle);
1656                 if (ret != SQLITE_OK) {
1657                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to close db, ret %d: %s for daemon", ret, sqlite3_errmsg(g_db_handle));
1658                 }
1659                 g_db_handle = NULL;
1660
1661                 if (0 != remove(g_path)) {
1662                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
1663                         g_db_cnt = (g_db_cnt + 1) % 1000;
1664                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1665                 }
1666                 is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
1667                 if (true == is_connect) {
1668                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
1669                         is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
1670                         if (true == is_connect) {
1671                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
1672                                 if (0 != vc_db_restore_command()) {
1673                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command for daemon");
1674                                 }
1675                         }
1676                 } else {
1677                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
1678                         return VC_DB_ERROR_OPERATION_FAILED;
1679                 }
1680
1681                 g_ref_cnt++;
1682                 SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization after restore for daemon");
1683                 return 0;
1684         }
1685
1686         is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
1687         if (false == is_connect) {
1688                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file for daemon");
1689                 if (0 != remove(g_backup_path)) {
1690                         g_backup_db_cnt = (g_backup_db_cnt + 1) % 1000;
1691                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
1692                         snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
1693                 }
1694                 is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_backup_handle);
1695                 if (true == is_connect) {
1696                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
1697                         if (0 != vc_db_restore_command()) {
1698                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command for daemon");
1699                         }
1700                 }
1701         }
1702
1703         g_ref_cnt++;
1704
1705         SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization for daemon");
1706         return VC_DB_ERROR_NONE;
1707 }
1708
1709 int __vc_db_open_db(char** path, sqlite3** db_handle)
1710 {
1711         int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1712         if (ret != SQLITE_OK) {
1713                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db for daemon, path = %s, ret %d: %s", *path, ret, sqlite3_errmsg(*db_handle));
1714                 if (*db_handle) {
1715                         db_util_close(*db_handle);
1716                         *db_handle = NULL;
1717                 }
1718
1719                 free(*path);
1720                 *path = NULL;
1721                 return VC_DB_ERROR_OPERATION_FAILED;
1722         }
1723         return VC_DB_ERROR_NONE;
1724 }
1725
1726 bool __vc_db_connect_db(char** path, sqlite3** db_handle)
1727 {
1728         bool is_connect = false;
1729         int ret = __vc_db_open_db(path, db_handle);
1730         if (0 != ret) {
1731                 int cnt = 0;
1732                 while (cnt <= 5) {
1733                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1734
1735                         usleep(100000);
1736                         if (0 == __vc_db_open_db(path, db_handle)) {
1737                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
1738                                 is_connect = true;
1739                                 break;
1740                         }
1741                         cnt++;
1742                 }
1743         } else {
1744                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
1745                 is_connect = true;
1746         }
1747         return is_connect;
1748 }
1749
1750 int vc_db_initialize(void)
1751 {
1752         SLOG(LOG_INFO, vc_db_tag(), "DB on initialization");
1753
1754         if (0 < g_ref_cnt) {
1755                 g_ref_cnt++;
1756                 return VC_DB_ERROR_NONE;
1757         }
1758
1759         /* For voice control DB */
1760         g_path = (char*)calloc(256, sizeof(char));
1761         if (NULL == g_path) {
1762                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1763                 return VC_DB_ERROR_OUT_OF_MEMORY;
1764         }
1765         /* This should be changed to general DB space - TZ_USER_DB */
1766         snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1767
1768         /* For Backup DB */
1769         g_backup_path = (char*)calloc(256, sizeof(char));
1770         if (NULL == g_backup_path) {
1771                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1772                 return VC_DB_ERROR_OUT_OF_MEMORY;
1773         }
1774         snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
1775
1776         bool is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1777         if (false == is_connect) {
1778                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing");
1779                 if (0 != remove(g_path)) {
1780                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1781                         g_db_cnt = (g_db_cnt + 1) % 1000;
1782                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1783                 }
1784                 is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1785                 if (true == is_connect) {
1786                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
1787                         is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1788                         if (true == is_connect) {
1789                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
1790                                 if (0 != vc_db_restore_command()) {
1791                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1792                                 }
1793                         }
1794                 } else {
1795                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
1796                         return VC_DB_ERROR_OPERATION_FAILED;
1797                 }
1798         }
1799
1800         is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1801         if (false == is_connect) {
1802                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file");
1803                 if (0 != remove(g_backup_path)) {
1804                         g_backup_db_cnt = (g_backup_db_cnt + 1) % 1000;
1805                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
1806                         snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
1807                 }
1808                 is_connect = __vc_db_connect_db(&g_path, &g_db_backup_handle);
1809                 if (true == is_connect) {
1810                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup");
1811                         if (0 != vc_db_restore_command()) {
1812                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1813                         }
1814                 }
1815         }
1816
1817         g_ref_cnt++;
1818
1819         SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization");
1820         return VC_DB_ERROR_NONE;
1821 }
1822
1823 int vc_db_finalize(void)
1824 {
1825         if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
1826         if (0 != --g_ref_cnt)
1827                 return VC_DB_ERROR_NONE;
1828
1829         if (NULL != g_path) {
1830                 free(g_path);
1831                 g_path = NULL;
1832         }
1833
1834         if (NULL != g_backup_path) {
1835                 free(g_backup_path);
1836                 g_backup_path = NULL;
1837         }
1838
1839         if (!g_db_handle)
1840                 return 0;
1841         db_util_close(g_db_handle);
1842         g_db_handle = NULL;
1843
1844         if (!g_db_backup_handle)
1845                 return 0;
1846         db_util_close(g_db_backup_handle);
1847         g_db_backup_handle = NULL;
1848
1849         return VC_DB_ERROR_NONE;
1850 }
1851
1852 int vc_db_create_table()
1853 {
1854         __vc_db_reset_handle();
1855         __vc_db_begin_transaction(g_db_handle);
1856
1857         int ret = __vc_db_create_table(g_db_handle, VC_INFO_TABLE);
1858         if (ret != VC_DB_ERROR_NONE) {
1859                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1860                 __vc_db_rollback_transaction(g_db_handle);
1861                 return VC_DB_ERROR_OPERATION_FAILED;
1862         }
1863         ret = __vc_db_create_table(g_db_handle, VC_RESULT_TABLE);
1864         if (ret != VC_DB_ERROR_NONE) {
1865                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1866                 __vc_db_rollback_transaction(g_db_handle);
1867                 return VC_DB_ERROR_OPERATION_FAILED;
1868         }
1869
1870         __vc_db_commit_transaction(g_db_handle);
1871         return VC_DB_ERROR_NONE;
1872 }
1873
1874 int __vc_db_delete_table(sqlite3* db_handle, const char* table)
1875 {
1876         char* sql = NULL;
1877         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1878                 sql = strdup("DELETE FROM vc_result;");
1879         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1880                 sql = strdup("DELETE FROM vc_info;");
1881         } else {
1882                 return VC_DB_ERROR_INVALID_PARAMETER;
1883         }
1884
1885         if (NULL == sql) {
1886                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1887                 return VC_DB_ERROR_OUT_OF_MEMORY;
1888         }
1889
1890         int ret = __vc_db_exec_query(db_handle, sql);
1891         if (ret != VC_DB_ERROR_NONE) {
1892                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1893                 free(sql);
1894                 sql = NULL;
1895                 return ret;
1896         }
1897
1898         free(sql);
1899         sql = NULL;
1900
1901         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1902                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1903         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1904                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1905         }
1906
1907         if (NULL == sql) {
1908                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1909                 return VC_DB_ERROR_OUT_OF_MEMORY;
1910         }
1911
1912         ret = __vc_db_exec_query(db_handle, sql);
1913         if (ret != VC_DB_ERROR_NONE) {
1914                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1915                 free(sql);
1916                 sql = NULL;
1917                 return ret;
1918         }
1919
1920         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1921
1922         free(sql);
1923         sql = NULL;
1924         return VC_DB_ERROR_NONE;
1925 }
1926
1927 int vc_db_delete_table(const char* table)
1928 {
1929         __vc_db_reset_handle();
1930         __vc_db_begin_transaction(g_db_handle);
1931
1932         int ret = __vc_db_delete_table(g_db_handle, table);
1933         if (0 != ret) {
1934                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1935                 __vc_db_rollback_transaction(g_db_handle);
1936                 return ret;
1937         }
1938
1939         __vc_db_commit_transaction(g_db_handle);
1940
1941         return VC_DB_ERROR_NONE;
1942 }
1943
1944 int vc_db_begin_transaction(void)
1945 {
1946         __vc_db_reset_handle();
1947         int ret = __vc_db_begin_transaction(g_db_handle);
1948         return ret;
1949 }
1950
1951 int vc_db_rollback_transaction(void)
1952 {
1953         __vc_db_reset_handle();
1954         int ret = __vc_db_rollback_transaction(g_db_handle);
1955         return ret;
1956 }
1957
1958 int vc_db_commit_transaction(void)
1959 {
1960         __vc_db_reset_handle();
1961         int ret = __vc_db_commit_transaction(g_db_handle);
1962         return ret;
1963 }
1964
1965 static void __vc_db_remove_space(char** string)
1966 {
1967         if (NULL == string || NULL == *string)
1968                 return;
1969
1970         char* temp = *string;
1971
1972         //remove previous space
1973         if (' ' == temp[0])
1974                 memmove(temp, temp + 1, strlen(temp));
1975
1976         // remove next space
1977         if (' ' == temp[strlen(temp) - 1])
1978                 temp[strlen(temp) - 1] = '\0';
1979 }
1980
1981 static bool __vc_db_is_valid_vfixed_string(char* string)
1982 {
1983         char* temp = strchr(string, '}');
1984         if (NULL == temp)
1985                 return false;
1986
1987         temp = strchr(string, '{');
1988         if (NULL == temp)
1989                 return false;
1990
1991         temp = strchr(string, '|');
1992         if (NULL == temp)
1993                 return false;
1994         return true;
1995 }
1996
1997 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1998 {
1999         if (NULL == cmd || NULL == cmd->command) {
2000                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
2001                 return VC_DB_ERROR_INVALID_PARAMETER;
2002         }
2003
2004         GSList* temp_list = NULL;
2005         char* temp = NULL;
2006         char* src_cmd = strdup(cmd->command);
2007         char* dst_cmd = NULL;
2008         char merge_cmd[256] = {0, };
2009
2010         if (NULL == src_cmd) {
2011                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
2012                 return VC_DB_ERROR_OUT_OF_MEMORY;
2013         }
2014
2015         if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
2016                 // check string validation
2017                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
2018                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
2019                         free(src_cmd);
2020                         src_cmd = NULL;
2021                         return VC_DB_ERROR_INVALID_PARAMETER;
2022                 }
2023
2024                 // remove close brace, '}'
2025                 char* temp_close = strchr(src_cmd, '}');
2026                 if (NULL != temp_close) {
2027                         temp_close[0] = '\0';
2028                 }
2029
2030                 // extract fixed command and remove space in front of  '{'
2031                 char *tok_ptr = NULL;
2032                 temp = strtok_r(src_cmd, "{", &tok_ptr);
2033                 if (NULL != temp) {
2034                         __vc_db_remove_space(&temp);
2035                         *fixed_cmd = strdup(temp);
2036
2037                         // merge command with fixed and vfixed
2038                         while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
2039                                 __vc_db_remove_space(&temp);
2040
2041                                 snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
2042                                 dst_cmd = strdup(merge_cmd);
2043                                 temp_list = g_slist_append(temp_list, dst_cmd);
2044                                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
2045                         }
2046                 } else {
2047                         *fixed_cmd = strdup(cmd->command);
2048                 }
2049         } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
2050                 // check string validation
2051                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
2052                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
2053                         free(src_cmd);
2054                         src_cmd = NULL;
2055                         return VC_DB_ERROR_INVALID_PARAMETER;
2056                 }
2057
2058                 // extract fixed command
2059                 char* temp_fixed = strchr(src_cmd, '}') + 1;
2060                 __vc_db_remove_space(&temp_fixed);
2061                 *fixed_cmd = strdup(temp_fixed);
2062
2063                 // remove close brace, '}'
2064                 char *tok_ptr = NULL;
2065                 temp = strtok_r(src_cmd, "}", &tok_ptr);
2066
2067                 // remove open brace, '{'
2068                 temp = strchr(src_cmd, '{') + 1;
2069
2070                 tok_ptr = NULL;
2071                 temp = strtok_r(temp, "|", &tok_ptr);
2072                 __vc_db_remove_space(&temp);
2073
2074                 // merge command with fixed and vfixed
2075                 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
2076                 dst_cmd = strdup(merge_cmd);
2077                 temp_list = g_slist_append(temp_list, dst_cmd);
2078                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
2079
2080                 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
2081                         __vc_db_remove_space(&temp);
2082
2083                         // merge command with fixed and vfixed
2084                         snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
2085                         dst_cmd = strdup(merge_cmd);
2086                         temp_list = g_slist_append(temp_list, dst_cmd);
2087                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
2088                 }
2089         } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
2090                 dst_cmd = strdup(src_cmd);
2091                 temp_list = g_slist_append(temp_list, dst_cmd);
2092                 *fixed_cmd = strdup(src_cmd);
2093
2094         } else {
2095                 dst_cmd = strdup(src_cmd);
2096                 temp_list = g_slist_append(temp_list, dst_cmd);
2097         }
2098
2099         *cmd_list = temp_list;
2100
2101         free(src_cmd);
2102         src_cmd = NULL;
2103         return VC_DB_ERROR_NONE;
2104 }
2105
2106 static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
2107 {
2108         GSList* cmd_list = NULL;
2109         char* fixed_cmd = NULL;
2110         vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
2111
2112         int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
2113         if (0 != ret) {
2114                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
2115
2116                 if (NULL != tmp_cmd) {
2117                         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
2118                 }
2119                 return ret;
2120         }
2121
2122         if (0 != g_slist_length(cmd_list)) {
2123                 GSList *iter = NULL;
2124                 char* temp_command = NULL;
2125                 iter = g_slist_nth(cmd_list, 0);
2126
2127                 while (NULL != iter) {
2128                         temp_command = iter->data;
2129
2130                         if (NULL != temp_command) {
2131                                 if (NULL != tmp_cmd->command) {
2132                                         free(tmp_cmd->command);
2133                                         tmp_cmd->command = NULL;
2134                                 }
2135                                 tmp_cmd->command = strdup(temp_command);
2136                                 if (NULL != fixed_cmd)
2137                                         tmp_cmd->fixed = strdup(fixed_cmd);
2138                                 else
2139                                         tmp_cmd->fixed = NULL;
2140
2141                                 ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
2142                                 if (ret != VC_DB_ERROR_NONE) {
2143                                         if (NULL != fixed_cmd) {
2144                                                 free(fixed_cmd);
2145                                                 fixed_cmd = NULL;
2146                                         }
2147                                         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
2148                                         break;
2149                                 }
2150
2151                                 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name && false == skip_invocation) {
2152                                         char temp[256] = {0, };
2153                                         snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
2154                                         if (NULL != tmp_cmd->command)
2155                                                 free(tmp_cmd->command);
2156
2157                                         tmp_cmd->command = strdup(temp);
2158
2159                                         ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
2160                                         if (ret != VC_DB_ERROR_NONE) {
2161                                                 if (NULL != fixed_cmd) {
2162                                                         free(fixed_cmd);
2163                                                         fixed_cmd = NULL;
2164                                                 }
2165                                                 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
2166                                                 break;
2167                                         }
2168                                 }
2169                                 cmd_list = g_slist_remove(cmd_list, temp_command);
2170                                 free(temp_command);
2171                                 temp_command = NULL;
2172                         }
2173                         iter = g_slist_nth(cmd_list, 0);
2174                 }
2175
2176                 if (VC_DB_ERROR_NONE != ret) {
2177                         while (NULL != iter) {
2178                                 temp_command = iter->data;
2179
2180                                 if (NULL != temp_command) {
2181                                         cmd_list = g_slist_remove(cmd_list, temp_command);
2182                                         free(temp_command);
2183                                         temp_command = NULL;
2184                                 }
2185
2186                                 iter = g_slist_nth(cmd_list, 0);
2187                         }
2188
2189                         g_slist_free(cmd_list);
2190                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
2191
2192                         return ret;
2193                 }
2194                 cmd_list = NULL;
2195         }
2196
2197         if (NULL != fixed_cmd) {
2198                 free(fixed_cmd);
2199                 fixed_cmd = NULL;
2200         }
2201         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
2202         return VC_DB_ERROR_NONE;
2203 }
2204
2205 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
2206 {
2207         int cnt = 0;
2208         int ret = -1;
2209         while (cnt < 5 && (ret = __vc_db_insert_command(g_db_handle, pid, type, cmd, skip_invocation))) {
2210                 usleep(100000);
2211                 SLOG(LOG_DEBUG, vc_db_tag(), "[ERROR] Fail to insert command(%d), retry cnt(%d)", ret, cnt);
2212                 cnt++;
2213         }
2214         return ret;
2215 }
2216
2217 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)
2218 {
2219         GSList *iter = NULL;
2220         vc_cmd_s *temp_cmd;
2221
2222         int i;
2223         int count = g_slist_length(cmd_list);
2224         iter = g_slist_nth(cmd_list, 0);
2225
2226         SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
2227
2228         for (i = 0; i < count; i++) {
2229                 if (NULL == iter)
2230                         break;
2231
2232                 temp_cmd = iter->data;
2233
2234                 if (NULL == temp_cmd) {
2235                         SLOG(LOG_ERROR, vc_db_tag(), "command is NULL");
2236                         break;
2237                 }
2238
2239                 if (type == temp_cmd->type) {
2240                         if (NULL != invocation_name)
2241                                 temp_cmd->invocation_name = strdup(invocation_name);
2242
2243                         int ret = __vc_db_insert_command(db_handle, pid, type, temp_cmd, skip_invocation);
2244                         if (ret != VC_DB_ERROR_NONE) {
2245                                 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
2246                                 return ret;
2247                         }
2248                 } else {
2249                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
2250                 }
2251                 iter = g_slist_next(iter);
2252         }
2253
2254         return VC_DB_ERROR_NONE;
2255 }
2256
2257 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
2258 {
2259         __vc_db_reset_handle();
2260         __vc_db_begin_transaction(g_db_handle);
2261
2262         int ret = __vc_db_insert_commands_list(g_db_handle, pid, type, cmd_list, invocation_name, false);
2263         if (ret != VC_DB_ERROR_NONE) {
2264                 __vc_db_rollback_transaction(g_db_handle);
2265                 return ret;
2266         }
2267
2268         __vc_db_commit_transaction(g_db_handle);
2269
2270         return VC_DB_ERROR_NONE;
2271 }
2272
2273 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
2274 {
2275         __vc_db_reset_handle();
2276         __vc_db_begin_transaction(g_db_handle);
2277
2278         int ret = __vc_db_get_commands(g_db_handle, pid, type, cmd_list);
2279         if (ret != VC_DB_ERROR_NONE) {
2280                 __vc_db_rollback_transaction(g_db_handle);
2281                 return ret;
2282         }
2283
2284         __vc_db_commit_transaction(g_db_handle);
2285         return VC_DB_ERROR_NONE;
2286 }
2287
2288 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
2289 {
2290         if (NULL == result_text) {
2291                 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
2292                 return VC_DB_ERROR_INVALID_PARAMETER;
2293         }
2294
2295         int ret = vc_db_delete_table(VC_RESULT_TABLE);
2296         if (0 != ret)
2297                 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2298
2299         if (NULL == vc_cmd_list) {
2300                 __vc_db_reset_handle();
2301                 __vc_db_begin_transaction(g_db_handle);
2302                 int ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, NULL);
2303                 if (ret != VC_DB_ERROR_NONE) {
2304                         __vc_db_rollback_transaction(g_db_handle);
2305                         return ret;
2306                 }
2307                 __vc_db_commit_transaction(g_db_handle);
2308                 return VC_DB_ERROR_NONE;
2309         }
2310
2311         /* Make client list node */
2312         vc_cmd_h vc_command = NULL;
2313         vc_cmd_list_first(vc_cmd_list);
2314
2315         while (VC_ERROR_ITERATION_END != ret) {
2316                 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
2317                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
2318                         break;
2319                 }
2320
2321                 if (NULL == vc_command) {
2322                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
2323                         break;
2324                 }
2325
2326                 vc_cmd_s* temp_cmd = NULL;
2327                 temp_cmd = (vc_cmd_s*)vc_command;
2328
2329                 __vc_db_reset_handle();
2330                 __vc_db_begin_transaction(g_db_handle);
2331                 ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, temp_cmd);
2332                 if (ret != VC_DB_ERROR_NONE) {
2333                         __vc_db_rollback_transaction(g_db_handle);
2334                         return ret;
2335                 }
2336                 __vc_db_commit_transaction(g_db_handle);
2337
2338                 ret = vc_cmd_list_next(vc_cmd_list);
2339         }
2340
2341         return VC_DB_ERROR_NONE;
2342 }
2343
2344 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
2345 {
2346         __vc_db_reset_handle();
2347         __vc_db_begin_transaction(g_db_handle);
2348
2349         int ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
2350         if (ret != VC_DB_ERROR_NONE) {
2351                 __vc_db_rollback_transaction(g_db_handle);
2352                 return VC_DB_ERROR_OPERATION_FAILED;
2353         }
2354
2355         if (NULL == msg) {
2356                 int count = 0;
2357                 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
2358                 if (ret != VC_DB_ERROR_NONE) {
2359                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
2360                 }
2361                 if (0 == count) {
2362                         char* appid = NULL;
2363                         // Get appid by pid using app control
2364                         ret = app_manager_get_app_id(pid, &appid);
2365                         if (APP_MANAGER_ERROR_NONE != ret) {
2366                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
2367                         }
2368                         ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
2369                         if (ret != VC_DB_ERROR_NONE) {
2370                                 __vc_db_rollback_transaction(g_db_handle);
2371                                 return ret;
2372                         }
2373                         if (NULL != appid) {
2374                                 free(appid);
2375                                 appid = NULL;
2376                         }
2377                 }
2378         }
2379         __vc_db_commit_transaction(g_db_handle);
2380         return VC_DB_ERROR_NONE;
2381 }
2382
2383 int vc_db_get_appid_list(const char* result, GSList** app_list)
2384 {
2385         __vc_db_reset_handle();
2386         __vc_db_begin_transaction(g_db_handle);
2387
2388         int ret = __vc_db_get_appid(g_db_handle, result, app_list);
2389         if (ret != VC_DB_ERROR_NONE) {
2390                 __vc_db_rollback_transaction(g_db_handle);
2391                 return ret;
2392         }
2393
2394         __vc_db_commit_transaction(g_db_handle);
2395         return VC_DB_ERROR_NONE;
2396 }
2397
2398 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
2399 {
2400         __vc_db_reset_handle();
2401         __vc_db_begin_transaction(g_db_handle);
2402
2403         int ret = __vc_db_get_result_pid_list(g_db_handle, result, pid_list);
2404         if (ret != VC_DB_ERROR_NONE) {
2405                 __vc_db_rollback_transaction(g_db_handle);
2406                 return ret;
2407         }
2408
2409         __vc_db_commit_transaction(g_db_handle);
2410         return VC_DB_ERROR_NONE;
2411 }
2412
2413 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
2414 {
2415         __vc_db_reset_handle();
2416         __vc_db_begin_transaction(g_db_handle);
2417
2418         int ret = __vc_db_append_commands(g_db_handle, pid, type, vc_cmd_list);
2419         if (ret != VC_DB_ERROR_NONE) {
2420                 __vc_db_rollback_transaction(g_db_handle);
2421                 return ret;
2422         }
2423
2424         __vc_db_commit_transaction(g_db_handle);
2425         return VC_DB_ERROR_NONE;
2426 }
2427
2428 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
2429 {
2430         __vc_db_reset_handle();
2431         __vc_db_begin_transaction(g_db_handle);
2432
2433         int ret = 0;
2434         ret = __vc_db_delete_commands(g_db_handle, pid, type, appid);
2435         if (ret != VC_DB_ERROR_NONE) {
2436                 __vc_db_rollback_transaction(g_db_handle);
2437                 return ret;
2438         }
2439
2440         __vc_db_commit_transaction(g_db_handle);
2441         return VC_DB_ERROR_NONE;
2442 }
2443
2444 int vc_db_backup_command()
2445 {
2446         GSList* list = NULL;
2447
2448         __vc_db_reset_handle();
2449         __vc_db_begin_transaction(g_db_backup_handle);
2450
2451         int ret = __vc_db_delete_table(g_db_backup_handle, VC_INFO_TABLE);
2452         if (0 != ret) {
2453                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2454                 __vc_db_rollback_transaction(g_db_backup_handle);
2455                 return ret;
2456         }
2457         __vc_db_commit_transaction(g_db_backup_handle);
2458
2459         ret = vc_db_get_commands(-1, VC_COMMAND_TYPE_BACKGROUND, &list);
2460         if (ret != VC_DB_ERROR_NONE) {
2461                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands");
2462                 return ret;
2463         }
2464
2465         __vc_db_begin_transaction(g_db_backup_handle);
2466
2467         ret = __vc_db_insert_commands_list(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2468         if (ret != VC_DB_ERROR_NONE) {
2469                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list to backup db");
2470                 __vc_db_rollback_transaction(g_db_backup_handle);
2471                 return ret;
2472         }
2473
2474         __vc_db_commit_transaction(g_db_backup_handle);
2475
2476         SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
2477         return VC_DB_ERROR_NONE;
2478 }
2479
2480 int vc_db_restore_command()
2481 {
2482         GSList* list = NULL;
2483
2484         __vc_db_reset_handle();
2485         __vc_db_begin_transaction(g_db_backup_handle);
2486
2487         int ret = __vc_db_get_commands(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, &list);
2488         if (ret != VC_DB_ERROR_NONE) {
2489                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands from backup db");
2490                 __vc_db_rollback_transaction(g_db_backup_handle);
2491                 return ret;
2492         }
2493         __vc_db_commit_transaction(g_db_backup_handle);
2494
2495         ret = vc_db_insert_commands_list(-1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2496         if (ret != VC_DB_ERROR_NONE) {
2497                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list");
2498                 return ret;
2499         }
2500
2501         SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
2502         return VC_DB_ERROR_NONE;
2503 }