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

#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include "vconf-internals.h"
#include <dirent.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <ctype.h>

#ifndef API
#define API __attribute__ ((visibility("default")))
#endif

#define VCONF_ERROR_RETRY_CNT 20
#define VCONF_ERROR_RETRY_SLEEP_UTIME 10000

#ifdef VCONF_USE_SQLFS_TRANSACTION
int IN_SBOX=0;

#define VCONF_MOUNT_PATH "/opt/var/kdb/db"
#define VCONF_MOUNT_PATH_CHECK \
do{\
	if(!IN_SBOX) \
      IN_SBOX = access("/opt/var/kdb/kdb_first_boot", F_OK) + 2; \
   	if(2==IN_SBOX) return 0;\
}while(0)

__thread int is_transaction;
#endif

#ifdef VCONF_TIMECHECK
double correction, startT;

double set_start_time(void)
{
	struct timeval tv;
	double curtime;

	gettimeofday(&tv, NULL);
	curtime = tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
	return curtime;
}

double exec_time(double start)
{
	double end = set_start_time();
	return (end - start - correction);
}

int init_time(void)
{
	double temp_t;
	temp_t = set_start_time();
	correction = exec_time(temp_t);

	return 0;
}
#endif

int _vconf_keynode_set_keyname(keynode_t *keynode, const char *keyname)
{
	if (keynode->keyname) free(keynode->keyname);
	keynode->keyname = strndup(keyname, VCONF_KEY_PATH_LEN);
	retvm_if(keynode->keyname == NULL, VCONF_ERROR, "strndup Fails");
	return VCONF_OK;
}

static inline void _vconf_keynode_set_dir(keynode_t *keynode)
{
	keynode->type = VCONF_TYPE_DIR;
}

static inline void _vconf_keynode_set_value_int(keynode_t *keynode, const int value)
{
	keynode->type = VCONF_TYPE_INT;
	keynode->value.i = value;
}

static inline void _vconf_keynode_set_value_bool(keynode_t *keynode, const int value)
{
	keynode->type = VCONF_TYPE_BOOL;
	keynode->value.b = !!value;
}

static inline void _vconf_keynode_set_value_dbl(keynode_t *keynode, const double value)
{
	keynode->type = VCONF_TYPE_DOUBLE;
	keynode->value.d = value;
}

static inline void _vconf_keynode_set_value_str(keynode_t *keynode, const char *value)
{
	keynode->type = VCONF_TYPE_STRING;
	keynode->value.s = strdup(value);
}

inline void _vconf_keynode_set_null(keynode_t *keynode)
{
	keynode->type = VCONF_TYPE_NONE;
	//keynode->value.d = NULL;
}

static inline keynode_t *_vconf_keynode_next(keynode_t *keynode)
{
	return keynode->next;
}

inline keynode_t *_vconf_keynode_new(void)
{
	keynode_t *keynode;
	keynode = calloc(1, sizeof(keynode_t));

	return keynode;
}

inline void _vconf_keynode_free(keynode_t *keynode)
{
	if(keynode) {
		if (keynode->keyname)
			free(keynode->keyname);
		if (keynode->type == VCONF_TYPE_STRING && keynode->value.s)
			free(keynode->value.s);
		free(keynode);
	}
}

static inline keynode_t *_vconf_keylist_headnode(keylist_t *keylist)
{
	return keylist->head;
}

static keynode_t *_vconf_keylist_lookup(keylist_t *keylist, const char *keyname,
				 keynode_t **before_keynode)
{
	keynode_t *found_node, *temp_node = NULL;
	size_t length = 1 + strlen(keyname);

	found_node = _vconf_keylist_headnode(keylist);

	while (found_node) {
		if(found_node->keyname == NULL) {
			ERR("key node has null keyname");
			return NULL;
		}

		if (!memcmp(keyname, found_node->keyname, length)) {
			if (before_keynode) {
					*before_keynode = temp_node;
			}
			return found_node;
		}

		temp_node = found_node;
		found_node = _vconf_keynode_next(found_node);
	}
	return NULL;
}

/*
 * This function get Key name of the keynode.
 * @param[in] keynode The Key
 * @return Key Name of the keynode
 */
API char *vconf_keynode_get_name(keynode_t *keynode)
{
	retvm_if(keynode == NULL, NULL, "Invalid argument: keynode is NULL");
	retvm_if(keynode->keyname == NULL, NULL, "The name of keynode is NULL");

	return keynode->keyname;
}

/*
 * This function get value type of the keynode.
 * @param[in] keynode The Key
 * @return Type of the keynode
 */
API int vconf_keynode_get_type(keynode_t *keynode)
{
	retvm_if(keynode == NULL, VCONF_ERROR, "Invalid argument: keynode is NULL");

	return keynode->type;
}

API int vconf_keynode_get_int(keynode_t *keynode)
{
	retvm_if(keynode == NULL, VCONF_ERROR, "Invalid argument: keynode is NULL");
	retvm_if(keynode->type != VCONF_TYPE_INT, VCONF_ERROR,
		 "The type(%d) of keynode(%s) is not INT", keynode->type, keynode->keyname);

	return keynode->value.i;
}

API double vconf_keynode_get_dbl(keynode_t *keynode)
{
	retvm_if(keynode == NULL, -1.0, "Invalid argument: keynode is NULL");
	retvm_if(keynode->type != VCONF_TYPE_DOUBLE, -1.0,
		 "The type(%d) of keynode(%s) is not DBL", keynode->type, keynode->keyname);

	return keynode->value.d;
}

/*
 * This function get Boolean value of the keynode.
 * @param[in] keynode The Key
 * @return Boolean value, -1 on error
 */
API int vconf_keynode_get_bool(keynode_t *keynode)
{
	retvm_if(keynode == NULL, VCONF_ERROR, "Invalid argument: keynode is NULL");
	retvm_if(keynode->type != VCONF_TYPE_BOOL, VCONF_ERROR,
		 "The type(%d) of keynode(%s) is not BOOL", keynode->type, keynode->keyname);

	return !!(keynode->value.b);
}

/*
 * This function get String value of the keynode.
 * @param[in] keynode The Key
 * @return String value, NULL on error
 */
API char *vconf_keynode_get_str(keynode_t *keynode)
{
	retvm_if(keynode == NULL, NULL, "Invalid argument: keynode is NULL");
	retvm_if(keynode->type != VCONF_TYPE_STRING, NULL,
		"The type(%d) of keynode(%s) is not STR", keynode->type, keynode->keyname);

	return keynode->value.s;
}

/*
 * Allocate, initialize and return a new Keylist object.
 * @return The pointer of New keylist, NULL on error
 */
API keylist_t *vconf_keylist_new(void)
{
	keylist_t *keylist;
	keylist = calloc(1, sizeof(keylist_t));

	return keylist;
}

/*
 * This function rewinds the KeyList internal cursor.
 * @param[in] keylist Key List
 * @return 0 on success, -1 on error
 */
API int vconf_keylist_rewind(keylist_t *keylist)
{
	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");

	keylist->cursor = NULL;

	return 0;
}

/*
 * A destructor for Keylist objects.
 * @param[in] keylist Key List
 * @return 0 on success, -1 on error
 */
API int vconf_keylist_free(keylist_t *keylist)
{
	keynode_t *keynode, *temp;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");

	if (keylist->num) {
		keynode = _vconf_keylist_headnode(keylist);
		while (keynode) {
			temp = _vconf_keynode_next(keynode);
			_vconf_keynode_free(keynode);
			keynode = temp;
		}
	}
	free(keylist);
	return 0;
}

/*
 * This function look for a Keynode contained in keylist that matches keyname.
 * @param[in] keylist Key List
 * @param[in] keyname Key to find
 * @param[out] return_node pointer of keynode to set
 * @return Type of the found key
 */
API int
vconf_keylist_lookup(keylist_t *keylist,
		     const char *keyname, keynode_t **return_node)
{
	keynode_t *found_node;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");
	retvm_if(keyname == NULL, VCONF_ERROR, "Invalid argument: keyname is NULL");
	retvm_if(return_node == NULL, VCONF_ERROR, "Invalid argument: return_node is NULL");

	found_node = _vconf_keylist_lookup(keylist, keyname, NULL);
	if (NULL == found_node)
		return 0;

	if (return_node)
		*return_node = found_node;
	return found_node->type;
}

/*
 * This function returns the next Key in a Keylist.
 * Next key is known by the keylist internal cursor.
 * @param[in] keylist Key List
 * @return The next Keynode, NULL on error
 */
API keynode_t *vconf_keylist_nextnode(keylist_t *keylist)
{
	retvm_if(keylist == NULL, NULL, "Invalid argument: keylist is NULL");

	if (keylist->cursor)
		keylist->cursor = _vconf_keynode_next(keylist->cursor);
	else
		keylist->cursor = keylist->head;

	return keylist->cursor;
}

/*
 * This function appends a new Keynode included integer value to the keylist.
 * If same keyname exist, the keynode will change.
 * @param[in] keylist Key List
 * @param[in] keyname Key
 * @param[in] value The integer value
 * @return Number of keynode included in the keylist, -1 on error
 */
API int
vconf_keylist_add_int(keylist_t *keylist, const char *keyname, const int value)
{
	keynode_t *keynode = NULL, *addition = NULL;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");
	retvm_if(keyname == NULL, VCONF_ERROR, "Invalid argument: keyname is NULL");

	if ((keynode = _vconf_keylist_lookup(keylist, keyname, NULL))) {
		_vconf_keynode_set_value_int(keynode, value);
		return keylist->num;
	}
	if ((keynode = _vconf_keylist_headnode(keylist)))
		while (_vconf_keynode_next(keynode))
			keynode = _vconf_keynode_next(keynode);

	addition = calloc(1, sizeof(keynode_t));
	retvm_if(addition == NULL, VCONF_ERROR, "(maybe)not enought memory");
	if (!_vconf_keynode_set_keyname(addition, keyname)) {
		_vconf_keynode_set_value_int(addition, value);
		if (keylist->head && NULL != keynode)
			keynode->next = addition;
		else
			keylist->head = addition;
		keylist->num += 1;
	} else {
		ERR("(maybe)not enought memory");
		free(addition), addition = NULL;
		return VCONF_ERROR;
	}

	return keylist->num;
}

/*
 * This function appends a new Keynode included boolean value to the keylist.
 * If same keyname exist, the keynode will change.
 * @param[in] keylist Key List
 * @param[in] keyname Key
 * @param[in] value The boolean value
 * @return Number of keynode included in the keylist, -1 on error
 */
API int
vconf_keylist_add_bool(keylist_t *keylist, const char *keyname, const int value)
{
	keynode_t *keynode = NULL, *addition = NULL;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");
	retvm_if(keyname == NULL, VCONF_ERROR, "Invalid argument: keyname is NULL");

	if ((keynode = _vconf_keylist_lookup(keylist, keyname, NULL))) {
		_vconf_keynode_set_value_bool(keynode, value);
		return keylist->num;
	}
	if ((keynode = _vconf_keylist_headnode(keylist)))
		while (_vconf_keynode_next(keynode))
			keynode = _vconf_keynode_next(keynode);

	addition = calloc(1, sizeof(keynode_t));
	retvm_if(addition == NULL, VCONF_ERROR, "(maybe)not enought memory");
	if (!_vconf_keynode_set_keyname(addition, keyname)) {
		_vconf_keynode_set_value_bool(addition, value);
		if (keylist->head && NULL != keynode)
			keynode->next = addition;
		else
			keylist->head = addition;
		keylist->num += 1;
	} else {
		ERR("(maybe)not enought memory");
		free(addition), addition = NULL;
		return VCONF_ERROR;
	}

	return keylist->num;
}

/*
 * This function appends a new Keynode included double value to the keylist.
 * If same keyname exist, the keynode will change.
 * @param[in] keylist Key List
 * @param[in] keyname Key
 * @param[in] value The double value
 * @return Number of keynode included in the keylist, -1 on error
 */
API int
vconf_keylist_add_dbl(keylist_t *keylist,
		      const char *keyname, const double value)
{
	keynode_t *keynode = NULL, *addition = NULL;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");
	retvm_if(keyname == NULL, VCONF_ERROR, "Invalid argument: keyname is NULL");

	if ((keynode = _vconf_keylist_lookup(keylist, keyname, NULL))) {
		_vconf_keynode_set_value_dbl(keynode, value);
		return keylist->num;
	}
	if ((keynode = _vconf_keylist_headnode(keylist)))
		while (_vconf_keynode_next(keynode))
			keynode = _vconf_keynode_next(keynode);

	addition = calloc(1, sizeof(keynode_t));
	retvm_if(addition == NULL, VCONF_ERROR, "(maybe)not enought memory");
	if (!_vconf_keynode_set_keyname(addition, keyname)) {
		_vconf_keynode_set_value_dbl(addition, value);
		if (keylist->head && NULL != keynode)
			keynode->next = addition;
		else
			keylist->head = addition;
		keylist->num += 1;
	} else {
		ERR("(maybe)not enought memory");
		free(addition), addition = NULL;
		return VCONF_ERROR;
	}

	return keylist->num;
}

/*
 * This function appends a new Keynode included string value to the keylist.
 * If same keyname exist, the keynode will change.
 * @param[in] keylist Key List
 * @param[in] keyname Key
 * @param[in] value The pointer of string value
 * @return Number of keynode included in the keylist, -1 on error
 */
API int
vconf_keylist_add_str(keylist_t *keylist,
		      const char *keyname, const char *value)
{
	keynode_t *keynode = NULL, *addition = NULL;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");
	retvm_if(keyname == NULL, VCONF_ERROR, "Invalid argument: keyname is NULL");

	if ((keynode = _vconf_keylist_lookup(keylist, keyname, NULL))) {
		if (VCONF_TYPE_STRING == keynode->type && keynode->value.s)
			free(keynode->value.s);
		_vconf_keynode_set_value_str(keynode, value);
		return keylist->num;
	}
	if (NULL != (keynode = _vconf_keylist_headnode(keylist)))
		while (_vconf_keynode_next(keynode))
			keynode = _vconf_keynode_next(keynode);

	addition = calloc(1, sizeof(keynode_t));
	retvm_if(addition == NULL, VCONF_ERROR, "(maybe)not enought memory");
	if (!_vconf_keynode_set_keyname(addition, keyname)) {
		_vconf_keynode_set_value_str(addition, value);
		if (keylist->head && NULL != keynode)
			keynode->next = addition;
		else
			keylist->head = addition;
		keylist->num += 1;
	} else {
		ERR("(maybe)not enought memory");
		free(addition), addition = NULL;
		return VCONF_ERROR;
	}

	return keylist->num;
}

/*
 * This function Appends a new Keynode to the keylist without value.
 * Use on vconf_get()
 * @param[in] keylist Key List
 * @param[in] keyname Key
 * @return Number of keynode included in the keylist, -1 on error
 */
API int vconf_keylist_add_null(keylist_t *keylist, const char *keyname)
{
	keynode_t *keynode = NULL, *addition = NULL;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");
	retvm_if(keyname == NULL, VCONF_ERROR, "Invalid argument: keyname is NULL");

	if (NULL != (keynode = _vconf_keylist_lookup(keylist, keyname, NULL))) {
		keynode->type = 0;
		keynode->value.d = 0;
		return keylist->num;
	}
	if ((keynode = _vconf_keylist_headnode(keylist)))
		while (_vconf_keynode_next(keynode))
			keynode = _vconf_keynode_next(keynode);

	addition = calloc(1, sizeof(keynode_t));
	retvm_if(addition == NULL, VCONF_ERROR, "(maybe)not enought memory");
	if (!_vconf_keynode_set_keyname(addition, keyname)) {
		if (keylist->head && keynode)
			keynode->next = addition;
		else
			keylist->head = addition;
		keylist->num += 1;
	} else {
		ERR("(maybe)not enought memory");
		free(addition), addition = NULL;
		return VCONF_ERROR;
	}

	return keylist->num;
}

/*
 * This function remove the keynode that matches keyname.
 * @param[in] keylist the keylist included the keyname
 * @param[in] keyname key
 * @return 0 on success, -1(Invalid parameter), -2(Not exist keyname in keylist) on error
 */
API int vconf_keylist_del(keylist_t *keylist, const char *keyname)
{
	keynode_t *found_node, *before_node = NULL;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");
	retvm_if(keyname == NULL, VCONF_ERROR, "Invalid argument: keyname is NULL");

	if ((found_node = _vconf_keylist_lookup(keylist, keyname, &before_node))) {
		if(before_node) {
			before_node->next = found_node->next;
		} else {
			/* requested key is headnode of keylist */
			keylist->head = found_node->next;
		}

		_vconf_keynode_free(found_node);
	} else
		return VCONF_ERROR;

	return VCONF_OK;
}


int _vconf_get_key_prefix(const char *keyname, int *prefix)
{
	if (strncmp(keyname, BACKEND_DB_PREFIX, strlen(BACKEND_DB_PREFIX)) == 0) {
		*prefix = VCONF_BACKEND_DB;
	} else if (0 == strncmp(keyname, BACKEND_FILE_PREFIX, strlen(BACKEND_FILE_PREFIX))) {
		*prefix = VCONF_BACKEND_FILE;
	} else if (0 == strncmp(keyname, BACKEND_MEMORY_PREFIX, strlen(BACKEND_MEMORY_PREFIX))) {
		*prefix = VCONF_BACKEND_MEMORY;
	} else {
		ERR("Invalid argument: wrong prefix of key(%s)", keyname);
		*prefix = VCONF_BACKEND_NULL;
		return VCONF_ERROR_WRONG_PREFIX;
	}

	return VCONF_OK;
}

int _vconf_get_key_path(const char *keyname, char *path)
{
	if (strncmp(keyname, BACKEND_DB_PREFIX, strlen(BACKEND_DB_PREFIX)) == 0) {
		snprintf(path, VCONF_KEY_PATH_LEN, "%s%s", BACKEND_SYSTEM_DIR, keyname);
	} else if (0 == strncmp(keyname, BACKEND_FILE_PREFIX, strlen(BACKEND_FILE_PREFIX))) {
		snprintf(path, VCONF_KEY_PATH_LEN, "%s%s", BACKEND_SYSTEM_DIR, keyname);
	} else if (0 == strncmp(keyname, BACKEND_MEMORY_PREFIX, strlen(BACKEND_MEMORY_PREFIX))) {
		snprintf(path, VCONF_KEY_PATH_LEN, "%s%s", BACKEND_MEMORY_DIR, keyname);
	} else {
		ERR("Invalid argument: wrong prefix of key(%s)", keyname);
		return VCONF_ERROR_WRONG_PREFIX;
	}

	return VCONF_OK;
}

#ifdef VCONF_USE_BACKUP_TRANSACTION
int _vconf_get_backup_path(const char *keyname, char *path)
{
	char key_buf[VCONF_KEY_PATH_LEN] = {0, };
	int i;

	for(i = 0; i<VCONF_KEY_PATH_LEN-1 && keyname[i] != '\0' ; i++) {
		if (keyname[i] == '/')
			key_buf[i] = '$';
		else
			key_buf[i] = keyname[i];
	}

	snprintf(path, VCONF_KEY_PATH_LEN, "%s%s%s%s", BACKEND_SYSTEM_DIR, BACKEND_DB_PREFIX, ".backup/", key_buf);

	return VCONF_OK;
}
#endif

#ifndef DISABLE_RUNTIME_KEY_CREATION
static int _vconf_set_key_check_parent_dir(const char* path)
{
	int exists = 0;
	struct stat stat_info;
	char* parent;
	char path_buf[VCONF_KEY_PATH_LEN] = {0,};
	int ret = 0;

	mode_t dir_mode =  0664 | 0111;

	parent = strrchr(path, '/');
	strncpy(path_buf, path, parent-path);
	path_buf[parent-path]=0;

	exists = stat(path_buf,&stat_info);
	if(exists){
		if(mkdir(path_buf, dir_mode) != 0) {
			if(errno == ENOENT) {
				ret = _vconf_set_key_check_parent_dir((const char*)path_buf);
				if(ret != VCONF_OK) return ret;
				if(mkdir(path_buf, dir_mode) != 0) {
					ERR("mkdir error(%d)", errno);
					return VCONF_ERROR;
				}
			}
		}
	}

	return VCONF_OK;
}

static int _vconf_set_key_creation(const char* path)
{
	int fd;
	mode_t temp;
	temp = umask(0000);
	fd = open(path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
	umask(temp);
	if(fd == -1) {
		ERR("open(rdwr,create) error\n");
		return VCONF_ERROR;
	}
	close(fd);

	return VCONF_OK;
}
#endif

static int _vconf_set_file_lock(int fd, short type)
{
	struct flock l;

	l.l_type = type; /*Do read Lock*/
	l.l_start= 0;	    /*Start at begin*/
	l.l_whence = SEEK_SET;
	l.l_len = 0;	    /*Do it with whole file*/

	return fcntl(fd, F_SETLKW, &l);
}

static int _vconf_set_read_lock(int fd)
{
	return _vconf_set_file_lock(fd, F_RDLCK);
}

static int _vconf_set_write_lock(int fd)
{
	return _vconf_set_file_lock(fd, F_WRLCK);
}

static int _vconf_set_unlock(int fd)
{
	return _vconf_set_file_lock(fd, F_UNLCK);
}

#ifdef VCONF_USE_SQLFS_TRANSACTION
static void _vconf_acquire_transaction_delay(int ms)
{
    struct timeval timeout;
    timeout.tv_sec = ms / 1000 ;
    timeout.tv_usec = 1000 * ( ms % 1000 );

    select(0, 0, 0, 0, &timeout);
}

static int _vconf_db_begin_transaction()
{
	pid_t value;
	VCONF_MOUNT_PATH_CHECK;

	if(is_transaction == 0) {
		value = getpid();
transaction_retry:
		if(setxattr(VCONF_MOUNT_PATH, "full_db_transaction_start", &value, sizeof(value), 0) == -1)
		{
			//ERR("someone access vconf. retrying...(%d)", errno);
			_vconf_acquire_transaction_delay(50);
			goto transaction_retry;
		} else {
			is_transaction++;
		}
	} else {
		is_transaction++;
	}

	return VCONF_OK;
}

static int _vconf_db_commit_transaction()
{
	pid_t value;
	VCONF_MOUNT_PATH_CHECK;

	retv_if(is_transaction <= 0, VCONF_ERROR);

	if(is_transaction == 1) {
		value = getpid();
transaction_retry:
		if(setxattr(VCONF_MOUNT_PATH, "full_db_transaction_stop", &value, sizeof(value), 0) == -1)
		{
			//ERR("setxattr failed. retrying...(%d)", errno);
			_vconf_acquire_transaction_delay(50);
			goto transaction_retry;
		} else {
			is_transaction = 0;
		}
	} else {
		is_transaction--;
	}

	return 0;
}

static int _vconf_db_rollback_transaction()
{
	pid_t value;
	VCONF_MOUNT_PATH_CHECK;

	retv_if(is_transaction <= 0, VCONF_ERROR);

	if(is_transaction == 1) {
		value = getpid();
transaction_retry:
		if(setxattr(VCONF_MOUNT_PATH, "full_db_transaction_rb", &value, sizeof(value), 0) == -1)
		{
			//ERR("setxattr failed. retrying...(%d)", errno);
			_vconf_acquire_transaction_delay(50);
			goto transaction_retry;
		} else {
			is_transaction = 0;
		}
	} else {
		is_transaction --;
	}

	return 0;
}
#endif //VCONF_USE_SQLFS_TRANSACTION

#ifdef VCONF_USE_BACKUP_TRANSACTION

static int _vconf_backup_check(char* vconf_path, char* backup_path)
{
	int ret = VCONF_OK;
	int vconf_fd = -1;
	int backup_fd = -1;
	char file_buf[BUF_LEN] = { 0, };
	char err_buf[100] = { 0, };
	int complete_mark = 0;
	int read_len;
	int err_no = 0;
	int is_recovered = 0;
	int file_size;

	if (access(vconf_path, F_OK) == -1 || access(backup_path, F_OK) == -1)
		return ret;

	vconf_fd = open(vconf_path, O_WRONLY);
	if (vconf_fd == -1) {
		ERR("open %s failed", vconf_path);
		ret = VCONF_ERROR_FILE_OPEN;
		err_no = errno;
		goto out_close;
	}

	if(_vconf_set_write_lock(vconf_fd) == -1) {
		ERR("set write lock %s failed", vconf_path);
		ret = VCONF_ERROR_FILE_LOCK;
		err_no = errno;
		goto out_close;
	}

	backup_fd = open(backup_path, O_RDONLY);
	if (backup_fd == -1) {
		ERR("open %s failed", backup_path);
		ret = VCONF_ERROR_FILE_OPEN;
		err_no = errno;
		goto out_unlock;
	}

	file_size = lseek(backup_fd, 0, SEEK_END);
	if (file_size == -1) {
		ERR("seek end %s failed", backup_path);
		ret = VCONF_ERROR_FILE_SEEK;
		err_no = errno;
		goto out_unlock;
	} else if (file_size < FILE_ATOMIC_GUARANTEE_SIZE + VCONF_BACKUP_COMP_MARK_SIZE) {
		goto out_unlock;
	}

	if (lseek(backup_fd, 0, SEEK_SET) != 0) {
		ERR("seek %s failed", backup_path);
		ret = VCONF_ERROR_FILE_SEEK;
		err_no = errno;
		goto out_unlock;
	}

	read_len = read(backup_fd, &complete_mark, sizeof(int));
	if (read_len < sizeof(int))
		goto out_unlock;

	if (complete_mark != VCONF_BACKUP_COMPLETE_MARK)
		goto out_unlock;

	if (ftruncate(vconf_fd, 0) == -1) {
		ERR("truncate %s failed", vconf_path);
		ret = VCONF_ERROR_FILE_TRUNCATE;
		err_no = errno;
		goto out_unlock;
	}

	while ( (read_len = read(backup_fd, file_buf, BUF_LEN )) >0) {
		if(write(vconf_fd, file_buf, read_len) != read_len) {
			ERR("write %s failed", backup_path);
			ret = VCONF_ERROR_FILE_WRITE;
			err_no = errno;
			goto out_unlock;
		}
	}

	is_recovered = 1;

out_unlock :
	if(_vconf_set_unlock(vconf_fd)==-1) {
		ERR("unset write lock %s failed", vconf_path);
		ret = VCONF_ERROR_FILE_LOCK;
		err_no = errno;
		goto out_close;
	}

out_close:
	if (vconf_fd != -1)
		close(vconf_fd);
	if(backup_fd != -1)
		close(backup_fd);

	if(ret == VCONF_OK) {
		if(remove(backup_path) == -1) {
			ret = VCONF_ERROR_FILE_REMOVE;
			err_no = errno;
		}
	}

	if(ret != VCONF_OK) {
		strerror_r(err_no, err_buf, 100);
		ERR("_vconf_backup_check failed %d (%d / %s)\n", ret, err_no, err_buf);
	} else if (is_recovered)
		WARN("vconf(%s) successfully recovered and backup file is removed", vconf_path, backup_path);
	else
		WARN("vconf(%s)'s non-complete backup file is removed", vconf_path, backup_path);

	return ret;
}

static int _vconf_backup_write_str(char* backup_path, char* value)
{
	int ret = VCONF_OK;
	char err_buf[100] = { 0, };
	int err_no = 0;
	int backup_fd = -1;
	int write_len = 0;
	int complete_mark = VCONF_BACKUP_COMPLETE_MARK;
	int empty_mark = 0;
	int vconf_type_str = VCONF_TYPE_STRING;
	int result;

	if( (backup_fd = open(backup_path, O_CREAT|O_EXCL|O_WRONLY, 0666)) == -1 ) {
		ERR("open %s failed", backup_path);
		ret = VCONF_ERROR_FILE_OPEN;
		err_no = errno;
		goto out_close;
	}

	if ((result = write(backup_fd, (void *)&empty_mark, sizeof(int))) != sizeof(int)) {
		ERR("write empty mark of %s failed %d", backup_path, result);
		ret = VCONF_ERROR_FILE_WRITE;
		err_no = errno;
		goto out_close;
	}

	if ((result = write(backup_fd, (void *)&vconf_type_str, sizeof(int))) != sizeof(int)) {
		ERR("write type of %s failed %d", backup_path, result);
		ret = VCONF_ERROR_FILE_WRITE;
		err_no = errno;
		goto out_close;
	}

	write_len = strlen(value);
	if((result = write(backup_fd, value, write_len)) != write_len) {
		ERR("write %s failed %d", backup_path, result);
		ret = VCONF_ERROR_FILE_WRITE;
		err_no = errno;
		goto out_close;

	}

	if (fdatasync(backup_fd) == -1) {
		ERR("sync %s failed", backup_path);
		ret = VCONF_ERROR_FILE_SYNC;
		err_no = errno;
		goto out_close;
	}

	if (lseek(backup_fd, 0, SEEK_SET) != 0) {
		ERR("seek %s failed", backup_path);
		ret = VCONF_ERROR_FILE_SEEK;
		err_no = errno;
		goto out_close;
	}

	if((result = write(backup_fd, (void *)&complete_mark, sizeof(int))) != sizeof(int)) {
		ERR("write complete mark of %s failed %d", backup_path, result);
		ret = VCONF_ERROR_FILE_WRITE;
		err_no = errno;
		goto out_close;
	}

	if (fdatasync(backup_fd) == -1) {
		ERR("sync %s failed", backup_path);
		ret = VCONF_ERROR_FILE_SYNC;
		err_no = errno;
		goto out_close;
	}

out_close:
	if(backup_fd != -1)
		close(backup_fd);

	if(ret != VCONF_OK) {
		strerror_r(err_no, err_buf, 100);
		ERR("_vconf_backup_write_str failed %d (%d / %s)\n", ret, err_no, err_buf);
	}

	return ret;

}

static int _vconf_backup_commit(char* backup_path)
{
	int ret = VCONF_OK;
	char err_buf[100] = { 0, };
	int err_no = 0;

	if(remove(backup_path) == -1) {
		ERR("remove %s failed", backup_path);
		ret = VCONF_ERROR_FILE_REMOVE;
		err_no = errno;
	}

	if(ret != VCONF_OK) {
		strerror_r(err_no, err_buf, 100);
		ERR("_vconf_backup_commit failed %d (%d / %s)\n", ret, err_no, err_buf);
	}

	return ret;

}
#endif // VCONF_USE_BACKUP_TRANSACTION

static int _vconf_set_key_filesys(keynode_t *keynode, int prefix)
{
	char path[VCONF_KEY_PATH_LEN] = {0,};
	FILE *fp = NULL;
	int ret = -1;
	int func_ret = VCONF_OK;
	int err_no = 0;
	char err_buf[100] = { 0, };
	int is_write_error = 0;
#ifdef VCONF_USE_BACKUP_TRANSACTION
	char backup_path[VCONF_KEY_PATH_LEN] = {0,};
	int is_backup_need = 0;
	int file_size = 0;
#endif

	errno = 0;

	ret = _vconf_get_key_path(keynode->keyname, path);
	retv_if(ret != VCONF_OK, ret);


#ifdef VCONF_CHECK_IS_INITIALIZED
	if(prefix == VCONF_BACKEND_MEMORY && VCONF_NOT_INITIALIZED) {
		func_ret = VCONF_ERROR_NOT_INITIALIZED;
		goto out_return;
	}
#endif

#ifdef VCONF_USE_BACKUP_TRANSACTION
	if(prefix == VCONF_BACKEND_DB && keynode->type == VCONF_TYPE_STRING) {
		_vconf_get_backup_path(keynode->keyname, backup_path);
		ret = _vconf_backup_check(path, backup_path);
		if(ret != VCONF_OK) {
			func_ret = ret;
			err_no = errno;
			goto out_return;
		}
	}
#endif

	if( (fp = fopen(path, "r+")) == NULL ) {
		func_ret = VCONF_ERROR_FILE_OPEN;
		err_no = errno;
		goto out_return;
	}

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if (prefix != VCONF_BACKEND_DB)
#endif
	{
		ret = _vconf_set_write_lock(fileno(fp));
		if(ret == -1) {
			func_ret = VCONF_ERROR_FILE_LOCK;
			err_no = errno;
			goto out_close;
		}
	}

#ifdef VCONF_USE_BACKUP_TRANSACTION
	if(prefix == VCONF_BACKEND_DB && keynode->type == VCONF_TYPE_STRING) {
		file_size = VCONF_TYPE_SIZE + strlen(keynode->value.s);
		if (file_size > FILE_ATOMIC_GUARANTEE_SIZE) {
			is_backup_need = 1;

			ret = _vconf_backup_write_str(backup_path, keynode->value.s);
			if(ret != VCONF_OK) {
				func_ret = ret;
				err_no = errno;
				goto out_unlock;
			} else
				WARN("vconf backup file for(%s) is created. file size(%d)", path, file_size);
		}
	}
#endif

	if (ftruncate(fileno(fp), 0) == -1) {
		func_ret = VCONF_ERROR_FILE_TRUNCATE;
		err_no = errno;
		goto out_unlock;
	}

	/* write key type */
	ret = fwrite((void *)&(keynode->type), sizeof(int), 1, fp);
	if(ret <= 0)
	{
		if(errno) {
			err_no = errno;
		} else {
			errno = EAGAIN;
		}
		func_ret = VCONF_ERROR_FILE_WRITE;
		goto out_unlock;
	}

	/* write key value */
	switch(keynode->type)
	{
		case VCONF_TYPE_INT:
			ret = fwrite((void *)&(keynode->value.i), sizeof(int), 1, fp);
			if(ret <= 0) is_write_error = 1;
			break;
		case VCONF_TYPE_DOUBLE:
			ret = fwrite((void *)&(keynode->value.d), sizeof(double), 1, fp);
			if(ret <= 0) is_write_error = 1;
			break;
		case VCONF_TYPE_BOOL:
			ret = fwrite((void *)&(keynode->value.b), sizeof(int), 1, fp);
			if(ret <= 0) is_write_error = 1;
			break;
		case VCONF_TYPE_STRING:
			ret = fprintf(fp,"%s",keynode->value.s);
			if(ret < strlen(keynode->value.s)) is_write_error = 1;
			//ret = fwrite((void *)keynode->value.s, sizeof(char), strlen(keynode->value.s), fp);
			break;
		default :
			func_ret = VCONF_ERROR_WRONG_TYPE;
			goto out_unlock;
	}
	if(is_write_error)
	{
		if(errno) {
			err_no = errno;
		} else {
			errno = EAGAIN;
		}
		func_ret = VCONF_ERROR_FILE_WRITE;
		goto out_unlock;
	}

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_FILE)
#else
	if(prefix == VCONF_BACKEND_FILE || prefix == VCONF_BACKEND_DB)
#endif
	{
		fflush(fp);

		ret = fdatasync(fileno(fp));
		if(ret == -1) {
			err_no = errno;
			func_ret = VCONF_ERROR_FILE_SYNC;
		}
	}

#ifdef VCONF_USE_BACKUP_TRANSACTION
	if (is_backup_need) {
		ret = _vconf_backup_commit(backup_path);
		if(ret != VCONF_OK) {
			func_ret = ret;
			err_no = errno;
			goto out_unlock;
		}
	}
#endif

out_unlock :
#ifdef VCONF_USE_SQLFS_TRANSACTION
	if (prefix != VCONF_BACKEND_DB)
#endif
	{
		ret = _vconf_set_unlock(fileno(fp));
		if(ret == -1) {
			func_ret = VCONF_ERROR_FILE_LOCK;
			err_no = errno;
			goto out_close;
		}
	}
out_close :
	fclose(fp);

out_return :
	if(err_no != 0) {
		strerror_r(err_no, err_buf, 100);
		ERR("_vconf_set_key_filesys(%d-%s) step(%d) failed(%d / %s)\n", keynode->type, keynode->keyname, func_ret, err_no, err_buf);
	}

	return func_ret;
}

static int _vconf_set_key(keynode_t *keynode)
{
	int func_ret = VCONF_OK;
	int ret = 0;
	int is_busy_err = 0;
	int retry = -1;
	int prefix = 0;
	int rc = 0;

	ret = _vconf_get_key_prefix(keynode->keyname, &prefix);
	retv_if(ret != VCONF_OK, ret);

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		_vconf_db_begin_transaction();
	}
#endif

	while((ret = _vconf_set_key_filesys(keynode, prefix)) != VCONF_OK)
	{
		is_busy_err = 0;
		retry++;

#ifdef VCONF_CHECK_INITIALIZED
		if(VCONF_NOT_INITIALIZED)
		{
			ERR("%s : vconf is not initialized\n", keynode->keyname);
			is_busy_err = 1;
		}
		else if(ret == VCONF_ERROR_FILE_OPEN)
#else

		if(ret == VCONF_ERROR_FILE_OPEN)
#endif
		{
			switch (errno)
			{
				/* file is not exist, make path */
#ifndef DISABLE_RUNTIME_KEY_CREATION
				case EFAULT :
				case ENOENT :
				{
					char path[VCONF_KEY_PATH_LEN] = {0,};
					rc = _vconf_get_key_path(keynode->keyname, path);
					if(rc != VCONF_OK) {
						ERR("_vconf_get_key_path error");
						break;
					}

					rc = _vconf_set_key_check_parent_dir(path);
					if(rc != VCONF_OK) {
						ERR("_vconf_set_key_check_parent_dir error : %s", path);
						break;
					}

					rc = _vconf_set_key_creation(path);
					if(rc != VCONF_OK) {
						ERR("_vconf_set_key_creation error : %s", path);
						break;
					}
					INFO("%s key is created", keynode->keyname);
				}
#endif
				case EAGAIN :
				case EMFILE :
				case ENFILE :
				case ETXTBSY :
				{
					is_busy_err = 1;
				}
			}
		}
		else if (ret == VCONF_ERROR_FILE_CHMOD)
		{
			switch (errno)
			{
				case EINTR :
				case EBADF :
				{
					is_busy_err = 1;
				}
			}
		}
		else if (ret == VCONF_ERROR_FILE_LOCK)
		{
			switch (errno)
			{
				case EBADF :
				case EACCES :
				case EAGAIN :
				case ENOLCK :
				{
					is_busy_err = 1;
				}
			}
		}
		else if (ret == VCONF_ERROR_FILE_WRITE)
		{
			switch (errno)
			{
				case 0 :
				case EAGAIN :
				case EINTR :
				case EIO :
				case ENOMEM :
				{
					is_busy_err = 1;
				}
			}
		}
		else
		{
			is_busy_err = 0;
		}

		if ((is_busy_err == 1) && (retry < VCONF_ERROR_RETRY_CNT)) {
			ERR("%s : write buf error(%d). write will be retried(%d) , usleep time : %d\n", keynode->keyname, ret, retry, (retry)*VCONF_ERROR_RETRY_SLEEP_UTIME);
			usleep((retry)*VCONF_ERROR_RETRY_SLEEP_UTIME);
			continue;
		} else {
			ERR("%s : write buf error(%d). break. (%d)\n", keynode->keyname, ret, retry);
			func_ret = VCONF_ERROR;
			break;
		}
	}

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		if(func_ret == VCONF_ERROR) {
			_vconf_db_rollback_transaction();
		} else {
			_vconf_db_commit_transaction();
		}
	}
#endif

	return func_ret;
}

/*
 * This function set the value of given keys
 * @param[in] keylist the keylist which should contain changed keys
 * @return 0 on success, -1 on error
 */
API int vconf_set(keylist_t *keylist)
{
	START_TIME_CHECK

	keynode_t *got_node;

	int func_ret = VCONF_OK;
	int ret = 0;
	int prefix = 0;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is NULL");

	INFO("vconf_set (%d)START", keylist->num);

	got_node = _vconf_keylist_headnode(keylist);

	retvm_if(got_node == NULL, VCONF_ERROR, "Invalid argument: headnode is NULL");

	ret = _vconf_get_key_prefix(got_node->keyname, &prefix);
	retv_if(ret != VCONF_OK, ret);

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		_vconf_db_begin_transaction();
	}
#endif

	while (got_node != NULL) {
		ret = _vconf_set_key(got_node);
		if(ret != VCONF_OK) {
			func_ret = VCONF_ERROR;
		}

		got_node = _vconf_keynode_next(got_node);
	}

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		if(func_ret == VCONF_ERROR) {
			_vconf_db_rollback_transaction();
		} else {
			_vconf_db_commit_transaction();
		}
	}
#endif

	END_TIME_CHECK

	return func_ret;
}

API int vconf_sync_key(const char *in_key)
{
	START_TIME_CHECK

	int fd;
	char path[VCONF_KEY_PATH_LEN] = {0,};
	int ret = -1;

	ret = _vconf_get_key_path(in_key, path);
	if(ret != VCONF_OK) return VCONF_ERROR;

	fd = open(path, O_RDWR);
	if(fd == -1) return VCONF_ERROR;

	fsync(fd);
	close(fd);

	END_TIME_CHECK

	return 0;
}

/*
 * This function set the integer value of given key
 * @param[in]	in_key	key
 * @param[in]	intval integer value to set
 * @return 0 on success, -1 on error
 */
API int vconf_set_int(const char *in_key, const int intval)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is NULL");

	int func_ret = VCONF_OK;

	keynode_t* pKeyNode = _vconf_keynode_new();
	retvm_if(pKeyNode == NULL, VCONF_ERROR, "key malloc fail");

	func_ret = _vconf_keynode_set_keyname(pKeyNode, in_key);
	if(func_ret != VCONF_OK) {
		_vconf_keynode_free(pKeyNode);
		ERR("set key name error");
		return VCONF_ERROR;
	}
	_vconf_keynode_set_value_int(pKeyNode, intval);

	if (_vconf_set_key(pKeyNode) != VCONF_OK) {
		ERR("vconf_set_int(%d) : %s(%d) error", getpid(), in_key, intval);
		func_ret = VCONF_ERROR;
	} else{
		INFO("vconf_set_int(%d) : %s(%d) success", getpid(), in_key, intval);
	}

	_vconf_keynode_free(pKeyNode);

	END_TIME_CHECK

	return func_ret;
}

/*
* This function set the boolean value of given key
* @param[in]	in_key	key
* @param[in]	boolval boolean value to set
		(Integer value 1 is 'True', and 0 is 'False')
* @return 0 on success, -1 on error
*/
API int vconf_set_bool(const char *in_key, const int boolval)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is NULL");

	int func_ret = VCONF_OK;
	keynode_t* pKeyNode = _vconf_keynode_new();
	retvm_if(pKeyNode == NULL, VCONF_ERROR, "key malloc fail");

	func_ret = _vconf_keynode_set_keyname(pKeyNode, in_key);
	if(func_ret != VCONF_OK) {
		_vconf_keynode_free(pKeyNode);
		ERR("set key name error");
		return VCONF_ERROR;
	}
	_vconf_keynode_set_value_bool(pKeyNode, boolval);

	if (_vconf_set_key(pKeyNode) != VCONF_OK) {
		ERR("vconf_set_bool(%d) : %s(%d) error", getpid(), in_key, boolval);
		func_ret = VCONF_ERROR;
	} else {
		INFO("vconf_set_bool(%d) : %s(%d) success", getpid(), in_key, boolval);
	}

	_vconf_keynode_free(pKeyNode);

	END_TIME_CHECK

	return func_ret;
}

/*
 * This function set the double value of given key
 * @param[in]	in_key	key
 * @param[in]	dblval double value to set
 * @return 0 on success, -1 on error
 */
API int vconf_set_dbl(const char *in_key, const double dblval)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is NULL");

	int func_ret = VCONF_OK;
	keynode_t* pKeyNode = _vconf_keynode_new();
	retvm_if(pKeyNode == NULL, VCONF_ERROR, "key malloc fail");

	func_ret = _vconf_keynode_set_keyname(pKeyNode, in_key);
	if(func_ret != VCONF_OK) {
		_vconf_keynode_free(pKeyNode);
		ERR("set key name error");
		return VCONF_ERROR;
	}
	_vconf_keynode_set_value_dbl(pKeyNode, dblval);

	if (_vconf_set_key(pKeyNode) != VCONF_OK) {
		ERR("vconf_set_dbl(%d) : %s(%f) error", getpid(), in_key, dblval);
		func_ret = VCONF_ERROR;
	} else {
		INFO("vconf_set_dbl(%d) : %s(%f) success", getpid(), in_key, dblval);
	}

	_vconf_keynode_free(pKeyNode);

	END_TIME_CHECK

	return func_ret;
}

/*
 * This function set the string value of given key
 * @param[in]	in_key	key
 * @param[in]	strval string value to set
 * @return 0 on success, -1 on error
 */
API int vconf_set_str(const char *in_key, const char *strval)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is NULL");
	retvm_if(strval == NULL, VCONF_ERROR, "Invalid argument: value is NULL");

	int func_ret = VCONF_OK;
	keynode_t* pKeyNode = _vconf_keynode_new();
	retvm_if(pKeyNode == NULL, VCONF_ERROR, "key malloc fail");

	func_ret = _vconf_keynode_set_keyname(pKeyNode, in_key);
	if(func_ret != VCONF_OK) {
		_vconf_keynode_free(pKeyNode);
		ERR("set key name error");
		return VCONF_ERROR;
	}
	_vconf_keynode_set_value_str(pKeyNode, strval);

	if (_vconf_set_key(pKeyNode) != VCONF_OK) {
		ERR("vconf_set_str(%d) : %s(%s) error", getpid(), in_key, strval);
		func_ret = VCONF_ERROR;
	} else {
		INFO("vconf_set_str(%d) : %s(%s) success", getpid(), in_key, strval);
	}

	_vconf_keynode_free(pKeyNode);

	END_TIME_CHECK

	return func_ret;
}


#ifdef SUPPORT_ELEKTRA_VALUE_FORMAT
/* keyFileUnserialize function of ELEKTRA */
static int _vconf_get_key_elektra_format(keynode_t *keynode, FILE *fp)
{
	char version[10] = {0,};
	char type[5] = {0,};
	char comment[8] = {0,};
	char file_buf[BUF_LEN] = {0,};
	char *value = NULL;
	int value_size = 0;
	int err_no = 0;
	char err_buf[100] = { 0, };
	int func_ret = VCONF_OK;
	char tmp;

	INFO("_vconf_get_key_elektra_format start");

	rewind(fp);

	if (!fgets(version, sizeof(version), fp))
	{
		if(ferror(fp)) {
			err_no = errno;
		} else {
			err_no = EAGAIN;
		}
		func_ret = VCONF_ERROR_FILE_FGETS;
		goto out_return;
	}
	if (strncmp(version,"RG",2)) {
		func_ret = VCONF_ERROR_WRONG_TYPE;
		goto out_return;
	}

	if (!fgets(type, sizeof(type), fp))
	{
		if(ferror(fp)) {
			err_no = errno;
		} else {
			err_no = EAGAIN;
		}
		func_ret = VCONF_ERROR_FILE_FGETS;
		goto out_return;
	}

	if (!fgets(comment, sizeof(comment), fp))
	{
		if(ferror(fp)) {
			err_no = errno;
		} else {
			err_no = EAGAIN;
		}
		func_ret = VCONF_ERROR_FILE_FGETS;
		goto out_return;
	}

	while(fgets(file_buf, sizeof(file_buf), fp))
	{
		if(value) {
			value_size = value_size + strlen(file_buf);
			tmp = (char *) realloc(value, value_size);
			if(!tmp) {
				free(value);
				func_ret = VCONF_ERROR_NO_MEM;
				break;
			}
			value = tmp;
			strncat(value, file_buf, strlen(file_buf));
		} else {
			value_size = strlen(file_buf) + 1;
			value = (char *)malloc(value_size);
			if(value == NULL) {
				func_ret = VCONF_ERROR_NO_MEM;
				break;
			}
			memset(value, 0x00, value_size);
			strncpy(value, file_buf, strlen(file_buf));
		}
	}

	if(ferror(fp)) {
		err_no = errno;
		func_ret = VCONF_ERROR_FILE_FGETS;
	} else {
		if(value) {
			switch(atoi(type))
			{
				case VCONF_TYPE_INT:
				{
					_vconf_keynode_set_value_int(keynode, atoi(value));
					break;
				}
				case VCONF_TYPE_DOUBLE:
				{
					_vconf_keynode_set_value_dbl(keynode, atof(value));
					break;
				}
				case VCONF_TYPE_BOOL:
				{
					_vconf_keynode_set_value_bool(keynode, atoi(value));
					break;
				}
				case VCONF_TYPE_STRING:
				{
					_vconf_keynode_set_value_str(keynode, value);
					break;
				}
				default :
				{
					func_ret = VCONF_ERROR_WRONG_VALUE;
				}
			}
		} else {
			if(atoi(type) == VCONF_TYPE_STRING) {
				_vconf_keynode_set_value_str(keynode, "");
			} else {
				func_ret = VCONF_ERROR_WRONG_VALUE;
			}
		}
	}

out_return :
	if(err_no != 0) {
		strerror_r(err_no, err_buf, 100);
		ERR("_vconf_set_key_filesys(%d/%s) step(%d) failed(%d / %s)\n", keynode->type, keynode->keyname, func_ret, err_no, err_buf);
	}

	if(value) free(value);

	return func_ret;
}
#endif

static int _vconf_get_key_filesys(keynode_t *keynode, int prefix)
{
	char path[VCONF_KEY_PATH_LEN] = {0,};
	int ret = -1;
	int func_ret = VCONF_OK;
	char err_buf[100] = { 0, };
	int err_no = 0;
	int type = 0;
	FILE *fp = NULL;
#ifdef VCONF_USE_BACKUP_TRANSACTION
	char backup_path[VCONF_KEY_PATH_LEN] = {0,};
#endif

	errno = 0;

	ret = _vconf_get_key_path(keynode->keyname, path);
	retv_if(ret != VCONF_OK, ret);

#ifdef VCONF_CHECK_INITIALIZED
	if(prefix == VCONF_BACKEND_MEMORY && VCONF_NOT_INITIALIZED)
	{
		func_ret = VCONF_ERROR_NOT_INITIALIZED;
		goto out_return;
	}
#endif

#ifdef VCONF_USE_BACKUP_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		_vconf_get_backup_path(keynode->keyname, backup_path);
		ret = _vconf_backup_check(path, backup_path);
		if(ret != VCONF_OK) {
			func_ret = ret;
			err_no = errno;
			goto out_return;
		}
	}
#endif

	if( (fp = fopen(path, "r")) == NULL ) {
		func_ret = VCONF_ERROR_FILE_OPEN;
		err_no = errno;
		goto out_return;
	}

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if (prefix != VCONF_BACKEND_DB)
#endif
	{
		ret = _vconf_set_read_lock(fileno(fp));
		if(ret == -1) {
			func_ret = VCONF_ERROR_FILE_LOCK;
			err_no = errno;
			goto out_close;
		}
	}

	/* read data type */
	if(!fread((void*)&type, sizeof(int), 1, fp)) {
		if(ferror(fp)) {
			err_no = errno;
		} else {
			errno = EAGAIN;
		}
		func_ret = VCONF_ERROR_FILE_FREAD;
		goto out_unlock;
	}

	/* read data value */
	switch(type)
	{
		case VCONF_TYPE_INT:
		{
			int value_int = 0;
			if(!fread((void*)&value_int, sizeof(int), 1, fp)) {
				if(ferror(fp)) {
					err_no = errno;
				} else {
					errno = EAGAIN;
				}
				func_ret = VCONF_ERROR_FILE_FREAD;
				goto out_unlock;
			} else {
				_vconf_keynode_set_value_int(keynode, value_int);
			}

			break;
		}
		case VCONF_TYPE_DOUBLE:
		{
			double value_dbl = 0;
			if(!fread((void*)&value_dbl, sizeof(double), 1, fp)) {
				if(ferror(fp)) {
					err_no = errno;
				} else {
					errno = EAGAIN;
				}
				func_ret = VCONF_ERROR_FILE_FREAD;
				goto out_unlock;
			} else {
				_vconf_keynode_set_value_dbl(keynode, value_dbl);
			}

			break;
		}
		case VCONF_TYPE_BOOL:
		{
			int value_int = 0;
			if(!fread((void*)&value_int, sizeof(int), 1, fp)) {
				if(ferror(fp)) {
					err_no = errno;
				} else {
					errno = EAGAIN;
				}
				func_ret = VCONF_ERROR_FILE_FREAD;
				goto out_unlock;
			} else {
				_vconf_keynode_set_value_bool(keynode, value_int);
			}

			break;
		}
		case VCONF_TYPE_STRING:
		{
			char file_buf[BUF_LEN] = {0,};
			char *value = NULL;
			int value_size = 0;

			while(fgets(file_buf, sizeof(file_buf), fp))
			{
				if(value) {
					value_size = value_size + strlen(file_buf);
					value = (char *) realloc(value, value_size);
					if(value == NULL) {
						func_ret = VCONF_ERROR_NO_MEM;
						break;
					}
					strncat(value, file_buf, strlen(file_buf));
				} else {
					value_size = strlen(file_buf) + 1;
					value = (char *)malloc(value_size);
					if(value == NULL) {
						func_ret = VCONF_ERROR_NO_MEM;
						break;
					}
					memset(value, 0x00, value_size);
					strncpy(value, file_buf, strlen(file_buf));
				}
			}

			if(ferror(fp)) {
				err_no = errno;
				func_ret = VCONF_ERROR_FILE_FGETS;
			} else {
				if(value) {
					_vconf_keynode_set_value_str(keynode, value);
				} else {
					_vconf_keynode_set_value_str(keynode, "");
				}
			}
			if(value)
				free(value);

			break;
		}
		default :
#ifdef SUPPORT_ELEKTRA_VALUE_FORMAT
			func_ret = _vconf_get_key_elektra_format(keynode, fp);
#else
			func_ret = VCONF_ERROR_WRONG_TYPE;
#endif
	}

out_unlock :
#ifdef VCONF_USE_SQLFS_TRANSACTION
	if (prefix != VCONF_BACKEND_DB)
#endif
	{
		ret = _vconf_set_unlock(fileno(fp));
		if(ret == -1) {
			func_ret = VCONF_ERROR_FILE_LOCK;
			err_no = errno;
			goto out_close;
		}
	}

out_close :
	fclose(fp);

out_return :
	if(err_no != 0) {
		strerror_r(err_no, err_buf, 100);
		ERR("_vconf_get_key_filesys(%d-%s) step(%d) failed(%d / %s)\n", keynode->type, keynode->keyname, func_ret, err_no, err_buf);
	}

	return func_ret;
}


int _vconf_get_key(keynode_t *keynode)
{
	int func_ret = VCONF_OK;
	int ret = 0;
	int is_busy_err = 0;
	int retry = -1;
	int prefix = 0;

	ret = _vconf_get_key_prefix(keynode->keyname, &prefix);
	retv_if(ret != VCONF_OK, ret);

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		_vconf_db_begin_transaction();
	}
#endif

	while((ret = _vconf_get_key_filesys(keynode, prefix)) != VCONF_OK)
	{
		is_busy_err = 0;
		retry++;

#ifdef VCONF_CHECK_INITIALIZED
		if(VCONF_NOT_INITIALIZED)
		{
			ERR("%s : vconf is not initialized\n", keynode->keyname);
			is_busy_err = 1;
		}
		else if(ret == VCONF_ERROR_FILE_OPEN)
#else
		if(ret == VCONF_ERROR_FILE_OPEN)
#endif
		{
			switch (errno)
			{
				case EAGAIN :
				case EMFILE :
				case ETXTBSY :
				{
					is_busy_err = 1;
				}
			}
		}
		else if (ret == VCONF_ERROR_FILE_LOCK)
		{
			switch (errno)
			{
				case EBADF :
				case EACCES :
				case EAGAIN :
				case ENOLCK :
				{
					is_busy_err = 1;
				}
			}
		}
		else if (ret == VCONF_ERROR_FILE_FREAD)
		{
			switch (errno)
			{
				case EAGAIN :
				case EINTR :
				case EIO :
				{
					is_busy_err = 1;
				}
			}
		}
		else
		{
			is_busy_err = 0;
		}

		if ((is_busy_err == 1) && (retry < VCONF_ERROR_RETRY_CNT)) {
			ERR("%s : read buf error(%d). read will be retried(%d) , %d\n", keynode->keyname, ret, retry, (retry)*VCONF_ERROR_RETRY_SLEEP_UTIME);
			usleep((retry)*VCONF_ERROR_RETRY_SLEEP_UTIME);
			continue;
		} else {
			ERR("%s : read buf error(%d). break\n",  keynode->keyname, ret);
			func_ret = VCONF_ERROR;
			break;
		}
	}

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		if(func_ret == VCONF_ERROR) {
			_vconf_db_rollback_transaction();
		} else {
			_vconf_db_commit_transaction();
		}
	}
#endif

	return func_ret;
}

/*
static int _vconf_check_value_integrity(const void *value, int type)
{
	int i = 0;

	if ((type == VCONF_TYPE_STRING) && (value != NULL)) {
		return 0;
	}

	if ((value) && (strlen(value) > 0)) {
		if ((type == VCONF_TYPE_INT) ||
			(type == VCONF_TYPE_BOOL)||
			(type == VCONF_TYPE_DOUBLE)) {
			while (*(((char *)value) + i) != '\0') {
				if ( !isdigit(*(((char *)value) + i)) ) {
					if ((type != VCONF_TYPE_BOOL) &&
						(*(((char *)value) + i) != '-')) {
						if ((type == VCONF_TYPE_DOUBLE) &&
							(*(((char *)value) + i) != '.')) {
							ERR("ERROR : vconf value is not digit.");
							return -1;
						}
					}
				}
				i++;
			}
		}

		return 0;
	} else {
		ERR("ERROR : vconf value is NULL.");
		return -2;
	}
}
*/

int _vconf_path_is_dir(char* path)
{
	struct stat entryInfo;

	if(lstat(path, &entryInfo) == 0 ) {
		if( S_ISDIR( entryInfo.st_mode ) ) {
			return 1;
		} else {
			return 0;
		}
	} else {
		return VCONF_ERROR;
	}
}

API int vconf_get(keylist_t *keylist, const char *dirpath, get_option_t option)
{
	DIR *dir = NULL;
	struct dirent entry;
	struct dirent *result = NULL;
	char full_file_path[VCONF_KEY_PATH_LEN] = {0,};
	char file_path[VCONF_KEY_PATH_LEN] = {0,};
	char full_path[VCONF_KEY_PATH_LEN] = {0,};
	char err_buf[ERR_LEN] = {0,};
	int rc = 0;
	int func_ret = 0;
	int ret = 0;
	int is_dir = 0;
	int prefix = 0;

	keynode_t *temp_keynode;

	retvm_if(keylist == NULL, VCONF_ERROR, "Invalid argument: keylist is null");
	retvm_if(dirpath == NULL, VCONF_ERROR, "Invalid argument: dirpath is null");

	temp_keynode = _vconf_keylist_headnode(keylist);

	if ((NULL != temp_keynode) && (VCONF_GET_KEY != option)) {
		ERR("Not support mode : Only VCONF_GET_KEY \
		option support To retrieve key with keylist");
		return VCONF_ERROR;
	}

	if(temp_keynode != NULL) {
		while(_vconf_keynode_next(temp_keynode)) {
			temp_keynode = _vconf_keynode_next(temp_keynode);
		}
	}

	ret = _vconf_get_key_path(dirpath, full_path);
	retvm_if(ret != VCONF_OK, ret, "Invalid argument: key is not valid");


	ret = _vconf_get_key_prefix(dirpath, &prefix);
	retv_if(ret != VCONF_OK, ret);

#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		_vconf_db_begin_transaction();
	}
#endif

	is_dir = _vconf_path_is_dir(full_path);
	if(is_dir == 1) {
		if((dir=opendir(full_path)) == NULL) {
			strerror_r(errno, err_buf, ERR_LEN);
			ERR("ERROR : open directory(%s) fail(%s)", dirpath, err_buf);
			func_ret = VCONF_ERROR;
			goto out_unlock;
		}

		if((readdir_r(dir, &entry, &result)) != 0) {
			strerror_r(errno, err_buf, ERR_LEN);
			ERR("ERROR : read directory(%s) fail(%s)", dirpath, err_buf);
			func_ret = VCONF_ERROR;
		}

		while(result != NULL)
		{
			if(( strcmp( entry.d_name, ".") == 0 ) || ( strcmp( entry.d_name, "..") == 0 )) {
		            goto NEXT;
			}

			keynode_t* keynode = _vconf_keynode_new();
			if(keynode == NULL) {
				closedir(dir);
				ERR("Invalid argument: key malloc fail");
				func_ret = VCONF_ERROR;
				goto out_unlock;
			}

			snprintf(file_path, VCONF_KEY_PATH_LEN, "%s/%s", dirpath, entry.d_name);
			snprintf(full_file_path, VCONF_KEY_PATH_LEN, "%s/%s", full_path, entry.d_name);

			rc = _vconf_path_is_dir(full_file_path);
			if(rc != VCONF_ERROR) {
				if(rc == 1) {
					/* directory */
					if(option == VCONF_GET_KEY) {
						_vconf_keynode_free(keynode);
						goto NEXT;
					} else {
						_vconf_keynode_set_keyname(keynode, file_path);
						_vconf_keynode_set_dir(keynode);
					}
				} else {
					_vconf_keynode_set_keyname(keynode, file_path);
					_vconf_get_key(keynode);
				}

				if (keylist->head && temp_keynode != NULL)
				{
					temp_keynode->next = keynode;
					temp_keynode = _vconf_keynode_next(temp_keynode);
				}
				else {
					keylist->head = keynode;
					temp_keynode = keylist->head;
				}
				keylist->num += 1;
			} else {
				_vconf_keynode_free(keynode);

				memset(err_buf, 0x00, sizeof(err_buf));
				strerror_r(errno, err_buf, sizeof(err_buf));
				ERR("ERROR : get path(%s) fail(%s)", file_path, err_buf);
				func_ret = VCONF_ERROR;
			}

	NEXT:
			if((readdir_r(dir, &entry, &result)) != 0) {
				memset(err_buf, 0x00, sizeof(err_buf));
				strerror_r(errno, err_buf, sizeof(err_buf));
				ERR("ERROR : read directory(%s) fail(%s)", dirpath, err_buf);
				func_ret = VCONF_ERROR;
			}
		}

		if((closedir(dir)) != 0) {
			memset(err_buf, 0x00, sizeof(err_buf));
			strerror_r(errno, err_buf, sizeof(err_buf));
			ERR("ERROR : close directory(%s) fail(%s)", dirpath, err_buf);
			func_ret = VCONF_ERROR;
		}
	} else if(is_dir == 0) {
		keynode_t* keynode = _vconf_keynode_new();
		retvm_if(keynode == NULL, VCONF_ERROR, "Invalid argument: key malloc fail");

		_vconf_keynode_set_keyname(keynode, dirpath);

		_vconf_get_key(keynode);

		if (keylist->head && temp_keynode != NULL) {
			temp_keynode->next = keynode;
			//temp_keynode = _vconf_keynode_next(temp_keynode);
		} else {
			keylist->head = keynode;
			temp_keynode = keylist->head;
		}
		keylist->num += 1;
	} else {
		func_ret = VCONF_ERROR;
		goto out_unlock;
	}
	vconf_keylist_rewind(keylist);

out_unlock:
#ifdef VCONF_USE_SQLFS_TRANSACTION
	if(prefix == VCONF_BACKEND_DB) {
		if(func_ret == VCONF_ERROR) {
			_vconf_db_rollback_transaction();
		} else {
			_vconf_db_commit_transaction();
		}
	}
#endif

	return func_ret;
}

/*
 * This function get the integer value of given key
 * @param[in]	in_key	key
 * @param[out]	intval output buffer
 * @return 0 on success, -1 on error
 */
API int vconf_get_int(const char *in_key, int *intval)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is null");
	retvm_if(intval == NULL, VCONF_ERROR, "Invalid argument: output buffer is null");

	int func_ret = VCONF_ERROR;
	keynode_t* pKeyNode = _vconf_keynode_new();
	retvm_if(pKeyNode == NULL, VCONF_ERROR, "key malloc fail");

	_vconf_keynode_set_keyname(pKeyNode, in_key);

	if (_vconf_get_key(pKeyNode) != VCONF_OK)
		ERR("vconf_get_int(%d) : %s error", getpid(), in_key);
	else {
		*intval = pKeyNode->value.i;

		if(pKeyNode->type == VCONF_TYPE_INT) {
			INFO("vconf_get_int(%d) : %s(%d) success", getpid(), in_key, *intval);
			func_ret = VCONF_OK;
		} else
			ERR("The type(%d) of keynode(%s) is not INT", pKeyNode->type, pKeyNode->keyname);
	}

	_vconf_keynode_free(pKeyNode);

	END_TIME_CHECK

	return func_ret;
}

/*
 * This function get the boolean value of given key
 * @param[in]	in_key	key
 * @param[out]	boolval output buffer
 * @return 0 on success, -1 on error
 */
API int vconf_get_bool(const char *in_key, int *boolval)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is null");
	retvm_if(boolval == NULL, VCONF_ERROR, "Invalid argument: output buffer is null");

	int func_ret = VCONF_ERROR;
	keynode_t* pKeyNode = _vconf_keynode_new();
	retvm_if(pKeyNode == NULL, VCONF_ERROR, "key malloc fail");

	_vconf_keynode_set_keyname(pKeyNode, in_key);

	if (_vconf_get_key(pKeyNode) != VCONF_OK)
		ERR("vconf_get_bool(%d) : %s error", getpid(), in_key);
	else {
		*boolval = !!(pKeyNode->value.b);

		if(pKeyNode->type == VCONF_TYPE_BOOL) {
			INFO("vconf_get_bool(%d) : %s(%d) success", getpid(), in_key, *boolval);
			func_ret = VCONF_OK;
		} else
			ERR("The type(%d) of keynode(%s) is not BOOL", pKeyNode->type, pKeyNode->keyname);
	}

	_vconf_keynode_free(pKeyNode);

	END_TIME_CHECK

	return func_ret;
}

/*
 * This function get the double value of given key
 * @param[in]	in_key	key
 * @param[out]	dblval output buffer
 * @return 0 on success, -1 on error
 */
API int vconf_get_dbl(const char *in_key, double *dblval)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is null");
	retvm_if(dblval == NULL, VCONF_ERROR, "Invalid argument: output buffer is null");

	int func_ret = VCONF_ERROR;
	keynode_t* pKeyNode = _vconf_keynode_new();
	retvm_if(pKeyNode == NULL, VCONF_ERROR, "key malloc fail");

	_vconf_keynode_set_keyname(pKeyNode, in_key);

	if (_vconf_get_key(pKeyNode) != VCONF_OK)
		ERR("vconf_get_dbl(%d) : %s error", getpid(), in_key);
	else {

		*dblval = pKeyNode->value.d;

		if(pKeyNode->type == VCONF_TYPE_DOUBLE) {
			INFO("vconf_get_dbl(%d) : %s(%f) success", getpid(), in_key, *dblval);
			func_ret = VCONF_OK;
		} else
			ERR("The type(%d) of keynode(%s) is not DBL", pKeyNode->type, pKeyNode->keyname);
	}

	_vconf_keynode_free(pKeyNode);

	END_TIME_CHECK

	return func_ret;
}

/*
 * This function get the string value of given key
 * @param[in]	in_key	key
 * @return pointer of key value on success, NULL on error
 */
API char *vconf_get_str(const char *in_key)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, NULL, "Invalid argument: key is null");

	keynode_t* pKeyNode = _vconf_keynode_new();
	retvm_if(pKeyNode == NULL, NULL, "key malloc fail");

	_vconf_keynode_set_keyname(pKeyNode, in_key);

	char *strval = NULL;
	char *tempstr = NULL;

	if (_vconf_get_key(pKeyNode) != VCONF_OK) {
		ERR("vconf_get_str(%d) : %s error", getpid(), in_key);
	} else {

		if(pKeyNode->type == VCONF_TYPE_STRING)
			tempstr = pKeyNode->value.s;
		else
			ERR("The type(%d) of keynode(%s) is not STR", pKeyNode->type, pKeyNode->keyname);

		if(tempstr)
			strval = strdup(tempstr);
		INFO("vconf_get_str(%d) : %s(%s) success", getpid(), in_key, strval);
	}

	_vconf_keynode_free(pKeyNode);

	END_TIME_CHECK

	return strval;
}

/*
 * This function unset given key
 * @param[in]	in_key	key
 * @return 0 on success, -1 on error
 */
API int vconf_unset(const char *in_key)
{
	START_TIME_CHECK

	char path[VCONF_KEY_PATH_LEN] = {0,};
	int ret = -1;
	int err_retry = VCONF_ERROR_RETRY_CNT;
	int func_ret = VCONF_OK;

	WARN("vconf_unset: %s. THIS API(vconf_unset) WILL BE DEPRECATED", in_key);

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is null");

	ret = _vconf_get_key_path(in_key, path);
	retvm_if(ret != VCONF_OK, VCONF_ERROR, "Invalid argument: key is not valid");

	retvm_if(access(path, F_OK) == -1, VCONF_ERROR, "Error : key(%s) is not exist", in_key);

	do {
		ret = remove(path);
		if(ret == -1) {
			ERR("vconf_unset error(%d) : %s", errno, in_key);
			func_ret = VCONF_ERROR;
		} else {
			func_ret = VCONF_OK;
			break;
		}
	} while(err_retry--);

	END_TIME_CHECK

	return func_ret;
}

/*
 * This function unset given key recursively
 * @param[in]	in_dir	Directory name for removing
 * @return 0 on success, -1 on error
 */
API int vconf_unset_recursive(const char *in_dir)
{
	START_TIME_CHECK

	DIR *dir = NULL;
	struct dirent entry;
	struct dirent *result = NULL;
	char fullpath[VCONF_KEY_PATH_LEN] = {0,};
	char dirpath[VCONF_KEY_PATH_LEN] = {0,};
	char err_buf[ERR_LEN] = {0,};
	int rc = 0;
	int func_ret = 0;
	int ret = 0;

	WARN("vconf_unset_recursive: %s. THIS API(vconf_unset_recursive) WILL BE DEPRECATED", in_dir);

	retvm_if(in_dir == NULL, VCONF_ERROR, "Invalid argument: dir path is null");

	ret = _vconf_get_key_path(in_dir, dirpath);
	retvm_if(ret != VCONF_OK, VCONF_ERROR, "Invalid argument: key is not valid");

	if((dir=opendir(dirpath)) == NULL) {
		strerror_r(errno, err_buf, ERR_LEN);
		ERR("ERROR : open directory(%s) fail(%s)", in_dir, err_buf);
		return VCONF_ERROR;
	}

	if((readdir_r(dir, &entry, &result)) != 0) {
		strerror_r(errno, err_buf, ERR_LEN);
		ERR("ERROR : read directory(%s) fail(%s)", in_dir, err_buf);
		func_ret = VCONF_ERROR;
	}

	while(result != NULL)
	{
		if(( strcmp( entry.d_name, ".") == 0 ) || ( strcmp( entry.d_name, "..") == 0 )) {
	            goto NEXT;
		}

		snprintf(fullpath,VCONF_KEY_PATH_LEN, "%s/%s", dirpath, entry.d_name);

		ret = _vconf_path_is_dir(fullpath);
		if(ret != VCONF_ERROR) {
			if(ret == 1) {
				rc = vconf_unset_recursive(fullpath);
				if(rc == VCONF_ERROR)
					func_ret = VCONF_ERROR;
			}

			rc = remove(fullpath);
			if(rc == -1) {
				memset(err_buf, 0x00, sizeof(err_buf));
				strerror_r(errno, err_buf, sizeof(err_buf));
				ERR("ERROR : remove path(%s) fail(%s)", in_dir, err_buf);
				func_ret = VCONF_ERROR;
			}
		} else {
			memset(err_buf, 0x00, sizeof(err_buf));
			strerror_r(errno, err_buf, sizeof(err_buf));
			ERR("ERROR : remove path(%s) fail(%s)", in_dir, err_buf);
			func_ret = VCONF_ERROR;
		}
NEXT:
		if((readdir_r(dir, &entry, &result)) != 0) {
			memset(err_buf, 0x00, sizeof(err_buf));
			strerror_r(errno, err_buf, sizeof(err_buf));
			ERR("ERROR : read directory(%s) fail(%s)", in_dir, err_buf);
			func_ret = VCONF_ERROR;
		}
	}

	if((closedir(dir)) != 0) {
		memset(err_buf, 0x00, sizeof(err_buf));
		strerror_r(errno, err_buf, sizeof(err_buf));
		ERR("ERROR : close directory(%s) fail(%s)", in_dir, err_buf);
		func_ret = VCONF_ERROR;
	}
#if 0
	if(func_ret == VCONF_OK) {
		if((remove(in_dir)) == -1) {
			memset(err_buf, 0x00, sizeof(err_buf));
			strerror_r(errno, err_buf, sizeof(err_buf));
			ERR("ERROR : remove path(%s) fail(%s)", in_dir, err_buf);
			func_ret = VCONF_ERROR;
		}
	}
#endif

	return func_ret;
}

API int vconf_notify_key_changed(const char *in_key, vconf_callback_fn cb, void *user_data)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is null");
	retvm_if(cb == NULL, VCONF_ERROR, "Invalid argument: cb(%p)", cb);

	if (_vconf_kdb_add_notify(in_key, cb, user_data)) {
		ERR("vconf_notify_key_changed : key(%s) add notify fail", in_key);
		return VCONF_ERROR;
	}

	INFO("vconf_notify_key_changed : %s noti is added", in_key);

	END_TIME_CHECK

	return VCONF_OK;
}

API int vconf_ignore_key_changed(const char *in_key, vconf_callback_fn cb)
{
	START_TIME_CHECK

	retvm_if(in_key == NULL, VCONF_ERROR, "Invalid argument: key is null");
	retvm_if(cb == NULL, VCONF_ERROR, "Invalid argument: cb(%p)", cb);

	if (_vconf_kdb_del_notify(in_key, cb)) {
		ERR("vconf_ignore_key_changed() failed: key(%s)", in_key);
		return VCONF_ERROR;
	}

	INFO("vconf_ignore_key_changed : %s noti removed", in_key);

	END_TIME_CHECK

	return VCONF_OK;
}

API mode_t vconf_set_permission(mode_t mode)
{
	/* TODO: implement! */
	return mode;
}

API int vconf_set_key_permission(const char *in_key, const mode_t mode)
{
	/* TODO: implement! */
	return 0;
}