summaryrefslogtreecommitdiff
path: root/src/dzl_dockerctl.c
blob: 2faa9d4382a4b67932e856691b545c41f9fa4202 (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
/**
  * @file        DZL_DockerCtl.c
 * @brief       Impelemetation of docker-egnine interfaces.
 *              Control for life cycle of dockzen including dockerd and swarm.

 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 * This software is the confidential and proprietary information
 * of Samsung Electronics, Inc. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Samsung.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>

#include "dockzen_types.h"
#include "dzl_internal_types.h"
#include "dzl_dockerctl.h"
#include "dzl_setting.h"
#include "dzl_dockercomm.h"
#include "dzl_dockerform.h"

#include "dzl_dockerctl.h"

#define PRE_INSTALLED_CONTAINER_PATH "/opt/beluga/container/"
#define MAX_WAIT_INTERVAL 150

#define DOCKER_SERVICE_UPDATE			"for i in $(docker service ls --format \"{{.Name}}\") ; do echo Name=$i ; docker ps --format \"{{.Image}}\" | grep $(docker service ls --filter name=$i --format \"{{.Image}}\") && echo \"skip\" || docker service update --force --detach=false --env-add UPDATE=0 $i ; done"
#define MAXLINE			1024
#define LOG_BUF_LEN		2048
#define GET_HTTP_PROXY_CMD			"source /opt/beluga/system/env/environment && echo $HTTP_PROXY"
#define GET_HTTPS_PROXY_CMD			"source /opt/beluga/system/env/environment && echo $HTTPS_PROXY"

dzl_dockerctl_status_table astSeviceStatusTbl[STATUS_MAX_NUM] = {
	{"new", CONTAINER_STATUS_CREATED},
	{"allocated", CONTAINER_STATUS_CREATED},
	{"pending", CONTAINER_STATUS_CREATED},
	{"assigned", CONTAINER_STATUS_CREATED},
	{"accepted", CONTAINER_STATUS_CREATED},
	{"preparing", CONTAINER_STATUS_CREATED},
	{"ready", CONTAINER_STATUS_CREATED},
	{"starting", CONTAINER_STATUS_CREATED},
	{"running", CONTAINER_STATUS_RUNNING},
	{"complete", CONTAINER_STATUS_EXITED},
	{"shutdown", CONTAINER_STATUS_EXITED},
	{"failed", CONTAINER_STATUS_EXITED},
	{"rejected", CONTAINER_STATUS_EXITED}
};

dzl_dockerctl_status_table astEventStatusTbl[EVEVT_STATUS_MAX_NUM] = {
	{"create", CONTAINER_STATUS_CREATED},
	{"start", CONTAINER_STATUS_RUNNING},
	{"die", CONTAINER_STATUS_EXITED},
	{"destroy", CONTAINER_STATUS_REMOVED}
};

status_monitor_cb dzl_dockerctl_callback;
int dzl_dockerctl_msg_queue_id;
dzl_dockerctl_status_init_e initialize_dzl_dockerctl_flag;

/**
 * @fn        static int __get_dzl_dockerctl_msg_queue_id(void)
 * @brief     This function to get message queue id
 * @return    int     id of msg queue
 */
static int __get_dzl_dockerctl_msg_queue_id(void)
{
	return dzl_dockerctl_msg_queue_id;
}

/**
 * @fn        static int __get_dzl_dockerctl_initialized(void)
 * @brief     This function to get flag of docker interface initialized
 *			      if docker interface initialized, it would return 1
 * @return    int     value of initialized value
 */
static dzl_dockerctl_status_init_e __get_dzl_dockerctl_initialized(void)
{
	return initialize_dzl_dockerctl_flag;
}

/**
 * @fn        __register_dzl_dockerctl_callback(status_monitor_cb handler)
 * @brief     This function to be registered callback function
 * @param     handler	[in] callback function
 * @return    void
 */
static docker_status_e __register_dzl_dockerctl_callback(status_monitor_cb handler)
{
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	if (DZL_DOCKERCTL_STATUS_INITIALIZED == __get_dzl_dockerctl_initialized())
		dzl_dockerctl_callback = handler;
	else
		ret = DOCKER_STATUS_UNKNOWN;

	return ret;
}

/**
 * @fn        static int __interface_send_message(dzl_dockerctl_msgq_buf_s *msg_buf)
 * @brief     This function to send message to deal with
 * @param     *msg_buf  [in] message buffer
 * @return    int    return of function
 */
static int __interface_send_message(dzl_dockerctl_msgq_buf_s * msg_buf)
{
	int msgqid;
	int msg_size;

	msg_buf->mtype = DOCKER_INTERFACE_QID;
	msg_size = sizeof(dzl_dockerctl_msgq_buf_s) - sizeof(msg_buf->mtype);

	msgqid = __get_dzl_dockerctl_msg_queue_id();
	_D("msgqid:%d", msgqid);

	if (msgsnd(msgqid, msg_buf, msg_size, 0) == -1) {
		_E("msgsnd error(%d)", errno);
		return -1;
	}

	return 0;
}

/**
 * @fn        static size_t __read_docker_event_callback(void *data, size_t size, size_t nmemb, void *buffer)
 * @brief     This function to be called when it receives callback
 * @param     *data   [out] message from callback
 * @param     size    [out] size of message buffer
 * @param     nmeb    [out] block of message
 * @param     *buffer	[out] user buffer
 * @return    size_t  the size of allocation
 */
static size_t __read_docker_event_callback(char *data, size_t size, size_t nmemb, void *buffer)
{
	size_t realsize = size * nmemb;
	struct buffer *mem = (struct buffer *)buffer;
	int size_data = 0;
	dzl_dockerctl_msgq_buf_s msg_buf;
	char *p = NULL;

	_D("call __read_callback");
	_D("size[%d] [%s]", (unsigned int)strlen(data), data);
	size_data = strlen(data);

	p = (char *)malloc(sizeof(char) * size_data + 1);
	if (p == NULL) {
		_D("mallock fail");
		return -1;
	}

	p[size_data] = 0;
	memcpy(p, data, size_data + 1);
	msg_buf.event_data = p;
	msg_buf.cmd = DZL_DOCKERCTL_CONTAINER_STATUS_CHANGE;
	__interface_send_message(&msg_buf);

	if (mem != NULL)
		free(NULL);

	return realsize;
}

/**
 * @fn        static int __get_services_info(dzl_dockerctl_ServicesInfo *pstServicesInfo)
 * @brief     This function to get services information from docker engine
 * @param     *pstServicesInfo	[inout] services information
 * @return    int   return of function
 */
static int __get_services_info(dzl_dockerctl_ServicesInfo * pstServicesInfo)
{
	DOCKER *services = NULL;
	CURLcode response;
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_SERVICES, retUrl, sizeof(retUrl)))
		return DOCKER_STATUS_UNKNOWN;

	services = dockercomm_docker_init();

	if (services) {
		response = dockercomm_docker_get(services, retUrl);

		if (response != CURLE_OK) {
			_D("docker curl response error!!\n");
			dockercomm_docker_destroy(services);
			return DOCKER_STATUS_ENGINE_API_FAIL;
		}

		if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(services))) {
			ret = dockerform_generate_servicesinfo(dockercomm_docker_buffer(services), pstServicesInfo);
			if (ret == DOCKER_STATUS_NO_ERROR) {
				_D("services request ok !!! [%d]", ret);
			} else {
				dockercomm_docker_destroy(services);
				if (pstServicesInfo->pstServices != NULL) {
					free(pstServicesInfo->pstServices);
					pstServicesInfo->pstServices = NULL;
				}

				return ret;
			}
		} else {
			_D("service fail !!!");
			_D("%s", dockercomm_docker_buffer(services));
			ret = DOCKER_STATUS_SERVICE_FAIL;
		}

		dockercomm_docker_destroy(services);
	} else {
		_D("getServicesInfo alloc fail");
		ret = DOCKER_STATUS_UNKNOWN;
	}

	_D("getServicesInfo return OK");

	return ret;
}

/**
 * @fn        static int __get_tasks_Info(dzl_dockerctl_TasksInfo *pstTasksInfo)
 * @brief     This function to get tasks information from docker engine
 * @param     *pstTasksInfo	[inout] tasks information
 * @return    int   return of function
 */
static int __get_tasks_Info(dzl_dockerctl_TasksInfo * pstTasksInfo)
{
	DOCKER *tasks = NULL;
	CURLcode response;
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_TASKS, retUrl, sizeof(retUrl)))
		return DOCKER_STATUS_UNKNOWN;

	tasks = dockercomm_docker_init();

	if (tasks) {
		response = dockercomm_docker_get(tasks, retUrl);

		if (response != CURLE_OK) {
			_D("docker curl response error!!\n");
			dockercomm_docker_destroy(tasks);
			return DOCKER_STATUS_SERVICE_FAIL;
		}

		if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(tasks))) {
			ret = dockerform_generate_tasksinfo(dockercomm_docker_buffer(tasks), pstTasksInfo);
			if (ret == DOCKER_STATUS_NO_ERROR) {
				_D("tasks request ok !!! [%d]", ret);
			} else {
				dockercomm_docker_destroy(tasks);
				if (pstTasksInfo->pstTasks != NULL) {
					free(pstTasksInfo->pstTasks);
					pstTasksInfo->pstTasks = NULL;
				}

				return ret;
			}
		} else {
			_D("tasks fail !!!");
			_D("%s", dockercomm_docker_buffer(tasks));
			ret = DOCKER_STATUS_SERVICE_FAIL;
		}

		dockercomm_docker_destroy(tasks);
	} else {
		_D("getTasksInfo alloc fail");
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

/**
 * @fn        static int __copy_ContainersInfo(dzl_dockerctl_ServicesInfo *pstServicesInfo, dzl_dockerctl_TasksInfo *pstTasksInfo, containers_info_s *pstContainersInfo)
 * @brief     This function to copy containers info from services inf and tasksinfo
 * @param     *pstServicesInfo		[inout] services information
 * @param     *pstTasksInfo			[inout] tasks information
 * @param     *pstContainersInfo	[inout] containers information
 * @return     int   return of function
 */
static int __copy_ContainersInfo(dzl_dockerctl_ServicesInfo * pstServicesInfo, dzl_dockerctl_TasksInfo * pstTasksInfo, containers_info_s * pstContainersInfo)
{
	int taskIdx = 0;
	int index = 0;
	int attr_len = 0;
	int ret = DOCKER_STATUS_NO_ERROR;

	// clear containers_info_s structure values into ZERO
	memset(pstContainersInfo, 0x0, sizeof(containers_info_s));

	if (pstServicesInfo == NULL || pstTasksInfo == NULL)
		return DOCKER_STATUS_UNKNOWN;

	_D("Count of Service [%d]", pstServicesInfo->count);
	_D("Count of Task [%d]", pstTasksInfo->count);

	if (pstServicesInfo->count != pstTasksInfo->count) {
		_D("count is wrong");
		return DOCKER_STATUS_UNKNOWN;
	}

	pstContainersInfo->count = pstServicesInfo->count;

	for (index = 0; index < pstContainersInfo->count; index++) {
		attr_len = strlen(pstServicesInfo->pstServices[index].id);
		if (attr_len > 0) {
			pstContainersInfo->container[index].id = (char *)malloc((attr_len + 1) * sizeof(char));
			if (pstContainersInfo->container[index].id != NULL) {
				memset(pstContainersInfo->container[index].id, 0x00, (attr_len + 1) * sizeof(char));
				memcpy(pstContainersInfo->container[index].id, pstServicesInfo->pstServices[index].id, (attr_len + 1));
			} else {
				ret = DOCKER_STATUS_UNKNOWN;
			}
		}
		attr_len = strlen(pstServicesInfo->pstServices[index].name);
		if (attr_len > 0) {
			pstContainersInfo->container[index].name = (char *)malloc((attr_len + 1) * sizeof(char));
			if (pstContainersInfo->container[index].name != NULL) {
				memset(pstContainersInfo->container[index].name, 0x00, (attr_len + 1) * sizeof(char));
				memcpy(pstContainersInfo->container[index].name, pstServicesInfo->pstServices[index].name, (attr_len + 1));
			} else {
				ret = DOCKER_STATUS_UNKNOWN;
			}
		}
		attr_len = strlen(pstServicesInfo->pstServices[index].imageName);
		if (attr_len > 0) {
			pstContainersInfo->container[index].image_name = (char *)malloc((attr_len + 1) * sizeof(char));
			if (pstContainersInfo->container[index].image_name) {
				memset(pstContainersInfo->container[index].image_name, 0x00, (attr_len + 1) * sizeof(char));
				memcpy(pstContainersInfo->container[index].image_name, pstServicesInfo->pstServices[index].imageName, (attr_len + 1));
			} else {
				ret = DOCKER_STATUS_UNKNOWN;
			}
		}
	}

	for (index = 0; index < pstContainersInfo->count; index++) {
		for (taskIdx = 0; taskIdx < pstTasksInfo->count; taskIdx++) {
			_D("C[%s]", pstContainersInfo->container[index].id);
			_D("T[%s]", pstTasksInfo->pstTasks[taskIdx].id);
			if (!strncmp(pstContainersInfo->container[index].id, pstTasksInfo->pstTasks[taskIdx].id, strlen(pstContainersInfo->container[index].id))) {
				attr_len = strlen(pstTasksInfo->pstTasks[taskIdx].status);
				if (attr_len > 0) {
					pstContainersInfo->container[index].status = (char *)malloc((attr_len + 1) * sizeof(char));
					if (pstContainersInfo->container[index].status) {
						memset(pstContainersInfo->container[index].status, 0x00, attr_len + 1);
						memcpy(pstContainersInfo->container[index].status, pstTasksInfo->pstTasks[taskIdx].status, (attr_len + 1));
						break;
					} else {
						ret = DOCKER_STATUS_UNKNOWN;
					}
				}
			}
		}
	}

	return ret;
}

/**
 * @fn        static int __leave_swarm(void)
 * @brief     This function to leave swarm
 *			  If swarm state error, it has to be left and re-init in order to set up swarm mode
 * @return   int   return of function
 */
static int __leave_swarm(void)
{
	DOCKER *docker;
	CURLcode response;
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	docker = dockercomm_docker_init();

	if (docker) {
		if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_SWARM_LEAVE, retUrl, sizeof(retUrl))) {
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_UNKNOWN;
		}

		_D("url[%s]", retUrl);
		response = dockercomm_docker_post_with_only_parameter(docker, retUrl, DOCKER_URL_SWARM_LEAVE_PARAMETER);
		if (response != CURLE_OK) {
			_D("docker curl error!!");
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_ENGINE_API_FAIL;
		}
		//In case of swarm, there is two cases, inactive or error
		//In case of inactive, it will not be worked for 'leave' but error case it has to leave at least once
		_D("swarm leave !!!");

		dockercomm_docker_destroy(docker);
	} else {
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

/**
 * @fn        static int __init_swarm(void)
 * @brief     This function to init swarm
 *			  If swarm state error, it has to be left and re-init in order to set up swarm mode
 * @return    int   return of function
 */
static int __init_swarm(void)
{
	DOCKER *docker;
	char *buf = NULL;
	int buf_size = 0;
	CURLcode response;
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	docker = dockercomm_docker_init();

	if (docker) {
		buf_size = dockerform_generate_swarm_init(&buf);
		if (buf != NULL) {
			_D("swarm data = %s", buf);
			if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_SWARM_INIT, retUrl, sizeof(retUrl))) {
				dockercomm_docker_destroy(docker);
				free(buf);
				buf = NULL;
				return DOCKER_STATUS_UNKNOWN;
			}

			response = dockercomm_docker_post(docker, retUrl, buf, buf_size, CURL_CONTENT_TYPE_JSON);
			free(buf);
			buf = NULL;

			if (response != CURLE_OK) {
				_D("docker curl error!!");
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_ENGINE_API_FAIL;
			}

			if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
				_D("swarm init !!!");
			} else {
				_D(dockercomm_docker_buffer(docker));
				_D("swarm init error !!!");
				ret = DOCKER_STATUS_SERVICE_FAIL;
			}

			dockercomm_docker_destroy(docker);
		} else {
			_D("buf NULL !!!");
			ret = DOCKER_STATUS_UNKNOWN;
		}
	} else {
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

/**
 * @fn        static int __check_swarm_service(void)
 * @brief     This function to check swarm services
 *			  Whether swarm is ready or not, it will check, if it is not ready, it would be left and re-init
 * @return    int   return of function
 */
static int __check_swarm_service(void)
{
	DOCKER *docker;
	CURLcode response;
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	docker = dockercomm_docker_init();

	if (docker) {

		if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_SWARM, retUrl, sizeof(retUrl))) {
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_UNKNOWN;
		}

		response = dockercomm_docker_get(docker, retUrl);
		if (response != CURLE_OK) {
			_D("docker curl error!!");
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_ENGINE_API_FAIL;
		}

		if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
			_D("Swarm is already init");
		} else {
			_D("Swarm has to be R-initialized");
			_D("%s", dockercomm_docker_buffer(docker));
			if (DOCKER_STATUS_NO_ERROR == __leave_swarm()) {
				if (DOCKER_STATUS_NO_ERROR == __init_swarm()) {
					_D("Swarm init OK");
				} else {
					_E("Swarm init Fail");
					ret = DOCKER_STATUS_SERVICE_FAIL;
				}
			} else {
				_E("Swarm leave error");
				ret = DOCKER_STATUS_SERVICE_FAIL;
			}
		}

		dockercomm_docker_destroy(docker);
	} else {
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

/**
 * @fn        static int dzl_dockerctl_enable_swarm_service(void)
 * @brief     This function to enable swarm services
 *			  Wrapper function to include swarm check / leave / init
 * @return    int   return of function
 */
int dzl_dockerctl_enable_swarm_service(void)
{
	int ret = DOCKER_STATUS_NO_ERROR;

	ret = __check_swarm_service();
	_D("check swarm [%d]", ret);

	return ret;
}

/**
 * @fn        static int __CLI_command(char *cmd, int get_output, char **ret_buf)
 * @brief     This function to run command for daemon
 * @param     *cmd   [in] command to run
 * @param     get_output   [in] the way to get ouput of the command
 * @param     * ret_buf   [in] output of the command
 * @return    int   return of function
 */
static docker_status_e __CLI_command(char *cmd, int get_output, char **ret_buf)
{
	FILE *stream = NULL;
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;
	int index = 0;
	int ch = 0;
	char *buf = NULL;

	if (strlen(cmd) > 0) {
		stream = popen(cmd, "r");
		if (stream != NULL) {
			switch (get_output) {
			case 0:			/* no message */
				//_D("cmd[%s] no output message !!", cmd);
				break;
			case 1:			/* to get a specific string */
				//_D("get a specific string");
				buf = (char *)malloc(MAXLINE * sizeof(char));
				if (buf != NULL) {
					memset(buf, 0x0, MAXLINE * sizeof(char));
					while ((index < MAXLINE) && ((ch = getc(stream)) != EOF) && (ch != '\n') && (ch != 13/* charage return */))
						buf[index++] = ch;
					//_D("buf:%s(%d)", buf, index);
				}
				break;
			case 2:			/* to get logs */
				//_D("get logs");
				buf = (char *)malloc(LOG_BUF_LEN * sizeof(char));
				if (buf != NULL) {
					memset(buf, 0x0, LOG_BUF_LEN * sizeof(char));
					while ((index < LOG_BUF_LEN) && ((ch = getc(stream)) != EOF))
						buf[index++] = ch;
					//_D("buf:%s(%d)", buf, index);
				}
				break;
			default:
				_E("no output message !!");
				break;
			}
			pclose(stream);
		} else {
			_E("popen error !!!");
		}
	} else
		ret = DOCKER_STATUS_INVALID_PARAM;

	if (buf)
		*ret_buf = buf;

	return ret;
}

/**
 * @fn        static int __check_dockerd_state(void)
 * @brief     This function to check docker engine state
 * @return    int   return of docker engine state
 */
static int __check_dockerd_state(void)
{
	CURLcode response;
	DOCKER *docker;
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	_D("check docker daemon !!!");

	docker = dockercomm_docker_init();

	if (docker) {
		if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_PING, retUrl, sizeof(retUrl))) {
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_UNKNOWN;
		}

		response = dockercomm_docker_get(docker, retUrl);

		if (response != CURLE_OK) {
			_E("curl Error!!");
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_ENGINE_API_FAIL;
		}

		_D("__check_dockerd_state = %s", dockercomm_docker_buffer(docker));

		if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
			_D("engine ok [%s]", dockercomm_docker_buffer(docker));
		} else {
			_E("Engine Fail");
			_E("%s", dockercomm_docker_buffer(docker));
			ret = DOCKER_STATUS_ENGINE_API_FAIL;
		}

		dockercomm_docker_destroy(docker);
	} else {
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

/**
 * @fn        static int __check_docker_engine_binary(char *path)
 * @brief     This function to check whether docker engine binary is exist or not
 * @param     *path   [in] dpath of docker engine binary
 * @return    int  result of function
 */
static int __check_docker_engine_binary(char *path)
{
	char daemon[256] = { 0, };
	int ret = 0;

	snprintf(daemon, sizeof(daemon), "%s/%s", path, DOCKER_DAEMON);

	ret = access(daemon, 0);

	if (ret != 0) {
		_D("no exists docker file");
		return -1;
	}

	_D("__check_docker_engine_binary ret[%d]", ret);
	return ret;
}

/**
 * @fn        static int __which_number(char *s)
 * @brief     This function to convert string value to int in range of number type(0-9)
 * @param     *s   [in] number string
 * @return    int   converted number from string
 */
static int __which_number(char *s)
{
	int len, i;
	len = strlen(s);

	for (i = 0; i < len; i++)
		if ((s[i] < '0' || s[i] > '9'))
			return -1;

	return atoi(s);
}

/**
 * @fn        static int __get_process_pid(char *str)
 * @brief     This function to get process id of docker engine
 * @param     *str   [in] string of finding daemon
 * @return    int  process id or -1 if it is not existed
 */
static int __get_process_pid(char *str)
{
	char cmd[100] = { 0, };
	char *ret_str = NULL;
	int pid = -1;
	int ret = 0;

	snprintf(cmd, sizeof(cmd), "pgrep -f %s", str);
	//_D("get_process_pid cmd = [%s](%d)", cmd, strlen(cmd));
	ret = __CLI_command(cmd, 1, &ret_str);
	if (ret_str != NULL) {
		pid = atoi(ret_str);
		free(ret_str);
	}
	return pid;
}

/**
 * @fn        static int __get_status_pid(int pid)
 * @brief     This function to get pid status
 * @param     pid   [in] pid number
 * @return    int   status of pid, 0(alive), -1(not exist)
 */
static int __get_status_pid(int pid)
{
	char path[100] = { 0, };
	FILE *fp;

	snprintf(path, 100, "/proc/%d/status", pid);

	fp = fopen(path, "r");
	if (fp == NULL) {
		_E("proc error !!");
		return -1;
	}

	fclose(fp);

	return 0;
}

static char *__generate_dockerd_env(void)
{
	char *proxy_cmd = NULL;
	char *ret_str = NULL;
	char http_proxy[512] = { 0, };
	char https_proxy[512] = { 0, };
	int cmd_len = 0;
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	// get http_proxy and https_proxy
	ret = __CLI_command(GET_HTTP_PROXY_CMD, 1, &ret_str);
	if (ret_str != NULL) {
		if (strlen(ret_str) > 0) {
			snprintf(http_proxy, sizeof(http_proxy), "http_proxy=\"http://%s\"", ret_str);
			cmd_len += strlen(http_proxy);
		}
		free(ret_str);
	}
	ret = __CLI_command(GET_HTTPS_PROXY_CMD, 1, &ret_str);
	if (ret_str != NULL) {
		if (strlen(ret_str) > 0) {
			snprintf(https_proxy, sizeof(https_proxy), "https_proxy=\"http://%s\"", ret_str);
			cmd_len += strlen(https_proxy);
		}
		free(ret_str);
	}

	if (cmd_len > 0) {
		proxy_cmd = (char *)malloc((cmd_len + 2) * sizeof(char));
		if (proxy_cmd != NULL) {
			snprintf(proxy_cmd, cmd_len + 2, "%s %s", http_proxy, https_proxy);
			//_D("proxy_cmd = [%s](%d)", proxy_cmd, strlen(proxy_cmd));
		}
	}

	return proxy_cmd;
}

/**
 * @fn        static docker_status_e __start_dockerd(docker_control_info_s *ctl_docker_p)
 * @brief     This function to start docker engine through cli command
 * @param     *ctl_docker_p   [in] path and options for docker engine to run
 * @return    docker_status_e  return of function
 */
static docker_status_e __start_dockerd(docker_control_info_s * ctl_docker_p)
{
	char command[512] = { 0, };
	char dockerd_cmd[256] = { 0, };
	int pid = 0;
	int index = 0;
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;
	char *proxy_cmd = NULL;
	/*
	   This function has to do a few things to get things done.
	   1) Whether docker engine is alive or not
	   1) Whether binary is existed or not
	   2) daemon is created or not
	   3) retry
	 */

	ret = __check_dockerd_state();
	if (ret == DOCKER_STATUS_NO_ERROR) {
		_D("already started dockerd");
		return ret;
	}

	if (0 != __check_docker_engine_binary(ctl_docker_p->path)) {
		_E("Not exists docker file");
		return DOCKER_STATUS_ENGINE_FAIL;
	}

	memset(dockerd_cmd, 0x00, sizeof(dockerd_cmd));
	if (dockerform_genetate_docker_cmd(ctl_docker_p, DOCKER_DAEMON, dockerd_cmd, sizeof(dockerd_cmd))) {
		_E("dockerform generate docker cmd has a problem");
		return DOCKER_STATUS_UNKNOWN;
	}

	/* get environments for docker daemon */
	proxy_cmd = __generate_dockerd_env();
	if (proxy_cmd != NULL) {
		snprintf(command, sizeof(command), "%s %s", proxy_cmd, dockerd_cmd);
		free(proxy_cmd);
	} else {
		snprintf(command, sizeof(command), "%s", dockerd_cmd);
	}
	_D("start dockerd command : [%s]", command);

	ret = __CLI_command(command, 0, NULL);
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_D("__CLI_command ret[%d]", ret);
		return ret;
	}

	/*  Check docker daemon is alive or not
	   Even though dockerd is okay, but pricoess id or status will not be opened, because of wrong parameter
	 */
	for (index = 0; index < DOCKER_ENGINE_RETRY_MAX; index++) {
		pid = __get_process_pid(DOCKER_DAEMON);
		if (pid == -1) {
			_E("dockerd process is not exist");
			ret = DOCKER_STATUS_ENGINE_FAIL;
		} else {
			if (-1 == __get_status_pid(pid)) {
				_E("docker process is removed");
				return DOCKER_STATUS_ENGINE_FAIL;
			} else {
				sleep(2);
				ret = DOCKER_STATUS_NO_ERROR;
				break;
			}
		}
		sleep(1);
	}

	// if launcher is restarted while container is running, we should update docker service.
	if (ret == DOCKER_STATUS_NO_ERROR) {
		_D("docker service update ~~~~~~~~~~~");
		ret = __CLI_command(DOCKER_SERVICE_UPDATE, 0, NULL);
		if (ret != DOCKER_STATUS_NO_ERROR) {
			_D("__CLI_command ret[%d]", ret);
			return ret;
		}
	}

	return ret;
}

/**
 * @fn        static int __load_image(char *filename)
 * @brief     This function to request loading image according to source of image
 * @param     *image   [in] whole name of file
 * @return    int      result of function
 */

static int __load_image(char *filename)
{
	DOCKER *docker;
	char retUrl[256] = { 0, };
	char *post_data = NULL;
	int post_data_size = 0;
	CURLcode response;
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	if (strlen(filename) > 0) {
		docker = dockercomm_docker_init();
		post_data_size = dockerform_generate_imageload_file(filename, &post_data);

		if (docker) {
			if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_IMAGE_LOAD, retUrl, sizeof(retUrl))) {
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_UNKNOWN;
			}

			_D("url[%s], filename[%s]", retUrl, filename);
			response = dockercomm_docker_post(docker, retUrl, post_data, post_data_size, CURL_CONTENT_TYPE_TAR);
			if (post_data != NULL) {
				free(post_data);
				post_data = NULL;
			}

			if (response != CURLE_OK) {
				_E("docker daemon error!!");
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_ENGINE_API_FAIL;
			}

			if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
				_D("load image request ok");
			} else {
				_E("Engine fail");
				_E("%s", dockercomm_docker_buffer(docker));
				ret = DOCKER_STATUS_ENGINE_API_FAIL;
			}

			dockercomm_docker_destroy(docker);
		} else {
			ret = DOCKER_STATUS_UNKNOWN;
		}
	} else {
		ret = DOCKER_STATUS_INVALID_PARAM;
		_D("load image parameter invalid");
	}

	if (post_data != NULL)
		free(post_data);

	_D("End of load image");
	return ret;
}

/**
 * @fn        static int __pull_image(char *image)
 * @brief     This function to request pulling image according to source of image
 * @param     *image   [in] whole name of image
 * @return    int   result of function
 */
static int __pull_image(char *image)
{
	DOCKER *docker;
	char retUrl[256] = { 0, };
	char imageName[256] = { 0, };
	CURLcode response;
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	if (strlen(image) > 0) {
		docker = dockercomm_docker_init();

		if (docker) {
			if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_IMAGE_CREATE, retUrl, sizeof(retUrl))) {
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_UNKNOWN;
			}

			snprintf(imageName, sizeof(imageName), "fromImage=%s", image);
			_D("url[%s], imageName[%s]", retUrl, imageName);
			response = dockercomm_docker_post_with_only_parameter(docker, retUrl, imageName);

			if (response != CURLE_OK) {
				_E("docker daemon error!!");
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_ENGINE_API_FAIL;
			}

			if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
				_D("pull image request ok");
			} else {
				_E("Engine fail");
				_E("%s", dockercomm_docker_buffer(docker));
				ret = DOCKER_STATUS_ENGINE_API_FAIL;
			}

			dockercomm_docker_destroy(docker);
		} else {
			ret = DOCKER_STATUS_UNKNOWN;
		}
	} else {
		ret = DOCKER_STATUS_INVALID_PARAM;
		_D(" pull image parameter invalid");
	}

	_D("End of pull image");
	return ret;
}

/**
 * @fn        static int __start_containers(container_create_info_s *info)
 * @brief     This function to request starting containers with create parameters
 * @param     *image   [in] creation parameters for container
 * @return    int  result of function
 */
static int __start_containers(container_create_info_s * info)
{
	char *ret_buf = NULL;
	int ret_buf_size = 0;
	char retUrl[256] = { 0, };
	CURLcode response;
	DOCKER *docker;
	int ret = DOCKER_STATUS_NO_ERROR;

	_D("Start container starts");
	ret_buf_size = dockerform_generate_imagecreate_data(info, &ret_buf);

	if (ret_buf != NULL) {
		docker = dockercomm_docker_init();
		if (docker != NULL) {
			if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_SERVICE_CREATE, retUrl, sizeof(retUrl))) {
				free(ret_buf);
				ret_buf = NULL;
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_UNKNOWN;
			}

			response = dockercomm_docker_post(docker, retUrl, ret_buf, ret_buf_size, CURL_CONTENT_TYPE_JSON);

			free(ret_buf);
			ret_buf = NULL;

			if (response != CURLE_OK) {
				_E("docker daemon error!!");
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_ENGINE_API_FAIL;
			}

			if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
				_D("Engine ok");
			} else {
				_E("Engine fail");
				_E("%s", dockercomm_docker_buffer(docker));
				ret = DOCKER_STATUS_ENGINE_API_FAIL;
			}

			dockercomm_docker_destroy(docker);
		} else {
			free(ret_buf);
			ret_buf = NULL;
			ret = DOCKER_STATUS_UNKNOWN;
		}
	} else {
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

/**
 * @fn        static int __check_service_with_name(char *name, dzl_dockerctl_serviceinfo_s *service_info)
 * @brief     This function to check service based on name
 * @param     *name   [in] name of service
 * @param     *service_info   [inout] structure of service to fill information
 * @return    int  result of function
 */
static int __check_service_with_name(char *name, dzl_dockerctl_serviceinfo_s * service_info)
{
	CURLcode response;
	DOCKER *docker;
	char requestUrl[256] = { 0, };
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	_D("__check_service_with_name !!!\n");

	if (strlen(name) == 0)
		return DOCKER_STATUS_INVALID_PARAM;

	_D("container check = [%s] \n", name);

	docker = dockercomm_docker_init();

	if (docker) {
		snprintf(requestUrl, (strlen(DOCKER_URL_SERVICES) + strlen(name) + 2), "%s/%s", DOCKER_URL_SERVICES, name);
		_D("requestUrl = %s", requestUrl);

		if (dockerform_generate_url(DOCKER_API_VERSION, requestUrl, retUrl, sizeof(retUrl))) {
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_UNKNOWN;
		}

		response = dockercomm_docker_get(docker, retUrl);

		if (response != CURLE_OK) {
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_ENGINE_API_FAIL;
		}

		if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
			_D("service ok");
			ret = dockerform_generate_service_withid_info(dockercomm_docker_buffer(docker), service_info);
			if (ret != DOCKER_STATUS_NO_ERROR) {
				dockercomm_docker_destroy(docker);
				return ret;
			}
		} else {
			_E("Engine Fail");
			ret = DOCKER_STATUS_ENGINE_API_FAIL;
			_D("%s", dockercomm_docker_buffer(docker));
		}

		dockercomm_docker_destroy(docker);
	} else {
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

/**
 * @fn        static container_status_e __find_service_status(char *status)
 * @brief     This function to find status according to astServiceStatusTbl
 *            Docker engine provides various status, but dockzen doesn't need to use all
 *            In case of service, it will retur service status, but has to be matched with own conainer status
 * @param     *status  [in] raw status
 * @return    int   converted container status from table
 */
static container_status_e __find_service_status(char *status)
{
	int index = 0;

	for (index = 0; index < STATUS_MAX_NUM; index++) {
		if (!strncmp(status, astSeviceStatusTbl[index].task_status, strlen(status)))
			return astSeviceStatusTbl[index].status;
	}

	return -1;
}

/**
 * @fn        static container_status_e __find_event_container_status(char *status)
 * @brief     This function to find status according to astEventStatusTbl
 *            Docker engine provides various status, but dockzen doesn't need to use all
 *            In case of event, it will returb container status, but has to be matched with own container status
 * @param     *status   [in] raw status
 * @return    int   converted container status from table
 */
static container_status_e __find_event_container_status(char *status)
{
	int index = 0;

	for (index = 0; index < EVEVT_STATUS_MAX_NUM; index++) {
		if (!strncmp(status, astEventStatusTbl[index].task_status, strlen(status)))
			return astEventStatusTbl[index].status;
	}

	return -1;
}

/**
 * @fn        static container_status_e __check_status_task_with_name(char *container_name, char *service_id, dzl_dockerctl_TasksInfo *tasks_info_p)
 * @brief     This function to request for checking task based on name
 * @param     *container_name  [in] container name
 * @param     *service_id  [in] service id
 * @param     *tasks_info_p			[in] task information
 * @return    int  container status
 */
static container_status_e __check_status_task_with_name(char *container_name, char *service_id, dzl_dockerctl_TasksInfo * tasks_info_p)
{
	int index = 0;
	int attr_len = 0;
	container_status_e status = CONTAINER_STATUS_UNKNOWN;

	if ((container_name == NULL || service_id == NULL) || tasks_info_p == NULL) {
		_D("__check_status_task_with_name Invalid params");
		return -1;
	}

	_D("ID[%s]", service_id);

	for (index = 0; index < tasks_info_p->count; index++) {
		_D("T[%s]", tasks_info_p->pstTasks[index].id);
		if (!strncmp(service_id, tasks_info_p->pstTasks[index].id, strlen(service_id))) {
			attr_len = strlen(tasks_info_p->pstTasks[index].status);
			if (attr_len > 0) {
				status = __find_service_status(tasks_info_p->pstTasks[index].status);
				if (status != -1) {
					_D("__check_status_task_with_name status [%d]", status);
					return status;
				}
			}
			break;
		}
	}

	return status;
}

/**
 * @fn        static int __update_image(dzl_dockerctl_serviceinfo_s *service_info, char *image)
 * @brief     This function to request for updating of image
 * @param     *service_info  [in] service information to generate update info
 * @param     *image  [in] image name to update
 * @return    int  result of function
 */
static int __update_image(dzl_dockerctl_serviceinfo_s * service_info, char *image)
{
	CURLcode response;
	DOCKER *docker;
	char *post_data = NULL;
	int post_data_size = 0;
	char updateName[256] = { 0, };
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	docker = dockercomm_docker_init();

	if (docker) {
		post_data_size = dockerform_generate_updateinfo(service_info, image, &post_data);

		if (post_data != NULL) {
			_D("service ID[%s]", service_info->id);
			_D("service Version[%d]", service_info->version);

			snprintf(updateName, sizeof(updateName), "%s/%s/update?registryAuthFrom=spec&version=%d", DOCKER_URL_SERVICES, service_info->id, service_info->version);

			if (dockerform_generate_url(DOCKER_API_VERSION, updateName, retUrl, sizeof(retUrl))) {
				free(post_data);
				post_data = NULL;
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_UNKNOWN;
			}

			response = dockercomm_docker_post(docker, retUrl, post_data, post_data_size, CURL_CONTENT_TYPE_JSON);
			free(post_data);

			if (response != CURLE_OK) {
				_E("curl Error!!\n");
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_ENGINE_API_FAIL;
			}

			if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
				_D("update request ok");
			} else {
				ret = DOCKER_STATUS_ENGINE_API_FAIL;
				_E("Engine Fail");
				_E("%s", dockercomm_docker_buffer(docker));
			}

		} else {
			ret = DOCKER_STATUS_UNKNOWN;
		}

		dockercomm_docker_destroy(docker);
	} else {
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

/**
 * @fn        static int __request_monitor_events(docker_event_cb callback)
 * @brief     This function to request monitor events and register callback function to be triggered
 * @param     callback  [in] callback function
 * @return    int  result of function
 */
static int __request_monitor_events(docker_event_cb callback)
{
	DOCKER *docker;
	CURLcode response;
	char retUrl[256] = { 0, };
	char filter[256] = { 0, };
	char opt_filter[100] = "filters={\"type\":{\"container\":true}}";
	int ret = DOCKER_STATUS_NO_ERROR;

	docker = dockercomm_docker_init();

	_D("start monitor event");
	if (docker) {
		snprintf(filter, sizeof(filter), "%s?%s", DOCKER_URL_EVENTS, opt_filter);
		if (dockerform_generate_url(DOCKER_API_VERSION, filter, retUrl, sizeof(retUrl))) {
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_UNKNOWN;
		}

		_D("url[%s]", retUrl);
		response = dockercomm_docker_get_chunked_data(docker, retUrl, callback);
		_D("finish event from curl");

		if (response != CURLE_OK) {
			_E("curl Error!!\n");
			dockercomm_docker_destroy(docker);
			return DOCKER_STATUS_ENGINE_API_FAIL;
		}

		dockercomm_docker_destroy(docker);
	}

	_D("end monitor event");

	return ret;
}

/**
 * @fn        static int __initialzie_docker_api(void)
 * @brief     This function to check docker engine api, it will check maximum 30 sec
 * @return    int  result of function
 */
static int __initialzie_docker_api(void)
{
	int index = 0;
	int ret = DOCKER_STATUS_NO_ERROR;

	for (index = 0; index < DOCKER_ENGINE_API_RETRY_MAX; index++) {
		ret = __check_dockerd_state();
		if (ret == DOCKER_STATUS_NO_ERROR) {
			break;
		} else if (ret == DOCKER_STATUS_UNKNOWN) {
			ret = DOCKER_STATUS_UNKNOWN;
			_D("__check_dockerd_state unknown [%d]", ret);
			break;
		} else {
			ret = DOCKER_STATUS_ENGINE_API_FAIL;
			_D("__check_dockerd_state [%d]", ret);
		}
		sleep(1);
	}

	return ret;
}

/**
 * @fn        static void __interface_server_loop(void)
 * @brief     This function to run main loop for interface server
 *			  It will be dealt with command
 * @return    void
 */
static void *__interface_server_loop(void *pv)
{
	docker_status_e ret = 0;
	int msgqid = 0;
	int msg_size = 0;
	int docker_status = 0;
	int container_status = 0;
	dzl_dockerctl_msgq_buf_s msg_buf;
	dzl_dockerctl_Task task_info = { 0, };

	_D("__interface_server_loop start\n");

	msgqid = __get_dzl_dockerctl_msg_queue_id();
	if (0 == msgqid) {
		_D("msgqid is zero");
		return NULL;
	}

	msg_size = sizeof(dzl_dockerctl_msgq_buf_s) - sizeof(msg_buf.mtype);

	while (1) {
		if (msgrcv(msgqid, &msg_buf, msg_size, 0, 0) == -1)
			_E("msgrcv error(%d)", errno);

		switch (msg_buf.cmd) {
		case DZL_DOCKERCTL_DOCKER_START:
			_D("DZL_DOCKERCTL_DOCKER_START");
			ret = dzl_dockerctl_enable_swarm_service();
			_D("swarm return ret[%d]", ret);
			docker_status = ret;
			dzl_dockerctl_callback(SM_TYPE_DOCKER_STATUS, &docker_status, NULL);
			break;
		case DZL_DOCKERCTL_CONTAINER_STATUS_CHANGE:
			_D("DZL_DOCKERCTL_CONTAINER_STATUS_CHANGE");
			char *p = msg_buf.event_data;

			if (!dockerform_parse_container_info(p, &task_info)) {
				container_status = __find_event_container_status(task_info.status);
				if (-1 != container_status) {
					_D("container_name:%s", task_info.id);
					_D("container_status:%d", container_status);
					_D("callback from docker interface");
					dzl_dockerctl_callback(SM_TYPE_CONTAINER_STATUS, task_info.id, &container_status);	// service name, status
				}
			}
			if (p != NULL)
				free(p);
			break;
		case DZL_DOCKERCTL_DOCKER_STATUS_CHANGE:
			_D("DZL_DOCKERCTL_DOCKER_STATUS_CHANGE");
			docker_status = __check_dockerd_state();
			_D("docker_status ret[%d]", ret);
			dzl_dockerctl_callback(SM_TYPE_DOCKER_STATUS, &docker_status, NULL);
			break;
		default:
			break;
		}
	}

	pthread_exit((void *)0);
	return NULL;
}

/**
 * @fn        static void __interface_event_loop(void)
 * @brief     This function to run event loop for checking event from docker engine
 *			  It will be dealt with events
 * @return    void
 */
static void *__interface_event_loop(void *pv)
{
	int ret = 0;
	int docker_status;
	dzl_dockerctl_msgq_buf_s msg_buf;

	_D("__interface_event_loop start\n");

	// In case of dockzen f/w is ready, it will be triggered to return f/w is idle
	docker_status = __initialzie_docker_api();
	msg_buf.cmd = DZL_DOCKERCTL_DOCKER_START;
	msg_buf.docker_status = docker_status;

	__interface_send_message(&msg_buf);
	if (docker_status != DOCKER_STATUS_NO_ERROR) {
		_E("__interface_event_loop error");
		pthread_exit((void *)0);
		return NULL;
	}

	/* In case of running time, check event from daemon.
	   It will be waited before disconnection the request and send message to notify engine stauts.
	   Because when the __request_monitor_events is finished which means docker daemon is not able to return
	   If the messge is sent, it whould be retry to start monitoring but the same message will not be triggered
	 */
	while (1) {
		_D("start monitor events");
		ret = __request_monitor_events(__read_docker_event_callback);
		_D("__monitor_events ret[%d]", ret);
		if (ret != DOCKER_STATUS_NO_ERROR) {
			msg_buf.cmd = DZL_DOCKERCTL_DOCKER_STATUS_CHANGE;
			msg_buf.docker_status = ret;
			__interface_send_message(&msg_buf);
		}

		sleep(1);
	}

	pthread_exit((void *)0);
	return NULL;
}

/**
 * @fn        static docker_status_e __init_interface_server(void)
 * @brief     This function to init interface server
 *			  check docker engine api server and create thread/message queue
 *			  If interface server is initialized, initialize_docker_if_flag will be set as 1
 * @return    docker_status_e  result of function
 */
static docker_status_e __init_interface_server(void)
{
	pthread_t p_thread;
	pthread_t p_event_thread;

	_D("__init_interface_server");

	if (DZL_DOCKERCTL_STATUS_INITIALIZED == __get_dzl_dockerctl_initialized()) {
		_W("Already initialized");
		return DOCKER_STATUS_NO_ERROR;
	}

	dzl_dockerctl_msg_queue_id = msgget((key_t) DOCKER_INTERFACE_QID, IPC_CREAT | 0666);

	_D("docker if queue %d", dzl_dockerctl_msg_queue_id);
	if (pthread_create(&p_thread, NULL, &__interface_server_loop, "dockzenif") < 0) {
		_E("__init_interface_server main create error");
		return DOCKER_STATUS_UNKNOWN;
	}

	if (pthread_create(&p_event_thread, NULL, &__interface_event_loop, "dockzenev") < 0) {
		_E("__init_interface_server event_thread create error");
		return DOCKER_STATUS_UNKNOWN;
	}

	initialize_dzl_dockerctl_flag = DZL_DOCKERCTL_STATUS_INITIALIZED;

	return DOCKER_STATUS_NO_ERROR;
}

/**
 * @fn        static int __update_task_history_1()
 * @brief     This function to set task history as 1
 *			  Set up the history of swarm task as 1, dockzen refers to the lastest task
 *        If swarm is ready, call this function  to set up
 * @return  int  result of function
 */
static docker_status_e __update_task_history_1(void)
{
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;
	ret = __CLI_command("docker swarm update --task-history-limit 1", 0, NULL);

	return ret;
}

static int __get_dockerd_path(char **dockerd_path)
{
	FILE *stream = NULL;
	char buf[128] = { 0, };
	char cmd[128] = { 0, };

	snprintf(cmd, sizeof(cmd), "type %s | sed -e 's;^.* ;;' | sed -e 's/\\/%s//g'", DOCKER_DAEMON, DOCKER_DAEMON);
	stream = popen(cmd, "r");

	if (stream == NULL) {
		_D("popen failed");
		return DOCKER_STATUS_UNKNOWN;
	} else {
		fgets(buf, 128, stream);

		*dockerd_path = (char *)malloc(strlen(buf) + 1);
		memset(*dockerd_path, 0x00, strlen(buf) + 1);
		if (strlen(buf) > 0)
			strncpy(*dockerd_path, buf, strlen(buf) - 1);	// "-1" means that last charactor of "/n" is removed
		_D("docker daemon path: %s(%d)", *dockerd_path, (unsigned int)strlen(buf));
	}
	pclose(stream);
	return DOCKER_STATUS_NO_ERROR;
}

static void __destory_dockerd_memory(docker_control_info_s * ctl_docker_p)
{
	if (ctl_docker_p != NULL) {
		if (ctl_docker_p->path != NULL)
			free(ctl_docker_p->path);

		free(ctl_docker_p);
	}

}

/**
 * @fn        static int __send_message(sm_msgq_buf_s *msg_buf)
 * @brief     This function to send message
 * @param     *msg_buf  [in] sm_sgq_buf structure to send message
 * @return    int  result of function
 */
static int __send_message(sm_msgq_buf_s * msg_buf)
{
	int msgqid;
	int msg_size;

	msg_buf->mtype = STATEMACHINE_QID;
	msg_size = sizeof(sm_msgq_buf_s) - sizeof(msg_buf->mtype);
	msgqid = msgget((key_t) STATEMACHINE_QID, IPC_CREAT | 0666);
	_D("msgqid:%d", msgqid);

	if (msgsnd(msgqid, msg_buf, msg_size, 0) == -1) {
		_E("msgsnd error(%d)", errno);
		return -1;
	}
	return 0;
}

static docker_status_e __docker_interface_load_image(char *filename)
{
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	if (access(filename, F_OK) == -1) {
		_D("no pre-installed image");
		return DOCKER_STATUS_INVALID_PARAM;
	}

	ret = __check_dockerd_state();
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_E("Docker Engine error");
		return ret;
	}

	ret = __load_image(filename);
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_E("load image error[%d]", ret);
		return ret;
	}

	return ret;
}

/*
*	load user containers from tar archive
*/
int dzl_dockerctl_pre_installed_container_load(void)
{
	int count, i, ret = -1;
	char file_name[FILE_PATH_MAX] = { 0, };

	count = dzl_setting_get_pre_installed_file_cnt();
	if (count == 0) {
		_E("there is no pre_installed container archive file");
	} else {
		_D("pre_installed image count : %d", count);
		for (i = 0; i < count; i++) {
			memset(file_name, 0x00, sizeof(file_name));
			ret = dzl_setting_get_pre_installed_filename(i, file_name);
			if (ret == 0) {
				_D("pre_installed_file_list[%d] : %s", i, file_name);
				if (__docker_interface_load_image(file_name) == DOCKER_STATUS_NO_ERROR)
					_D("image {%s} load success", file_name);
				else
					_E("image {%s} load fail", file_name);
			} else
				_E("dzl_setting_get_pre_installed_filename error!!!");
		}
		_D("pre_installed image loading completed!!!!");
	}
	return DOCKER_STATUS_NO_ERROR;
}

docker_status_e docker_interface_start(char *dockerd_opt_cmd, status_monitor_cb callback)
{
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;
	docker_control_info_s *ctl_docker_p = NULL;
	initialize_dzl_dockerctl_flag = DZL_DOCKERCTL_STATUS_NOT_READY;

	ctl_docker_p = (docker_control_info_s *) malloc(sizeof(docker_control_info_s));
	if (ctl_docker_p == NULL) {
		_E("alloc fail for ctl_docker_p");
		return DOCKER_STATUS_UNKNOWN;
	}

	memset(ctl_docker_p, 0x00, sizeof(docker_control_info_s));

	__get_dockerd_path(&ctl_docker_p->path);
	ctl_docker_p->opt_cmd = dockerd_opt_cmd;

	_D("ctl_docker_p->path: %s", ctl_docker_p->path);
	_D("ctl_docker_p->opt_cmd: %s", ctl_docker_p->opt_cmd);

	/*  Check docker daemon state first, if dockerd is already run, skip to run dockerd check interface server
	   Here is steps
	   1) start daemon
	   2) interface server
	   3) register callback
	   4) update task history to maintain only 1 container according to service
	 */
	do {
		ret = __start_dockerd(ctl_docker_p);
		if (ret != DOCKER_STATUS_NO_ERROR) {
			_E("__start_dockerd ret [%d]", ret);
			break;
		}

		ret = __init_interface_server();
		if (ret != DOCKER_STATUS_NO_ERROR) {
			_E("__init_interface_server ret [%d]", ret);
			break;
		}

		ret = __register_dzl_dockerctl_callback(callback);
		if (ret != DOCKER_STATUS_NO_ERROR) {
			_E("__init_interface_server ret [%d]", ret);
			break;
		}
		/*
		   update docker swarm option. default task history is et to 10.
		   as to simplify the parsing of containers info, we should set it as 1
		   during processing, if more than 1, error will be occured.
		 */
		ret = __update_task_history_1();
		if (ret != DOCKER_STATUS_NO_ERROR) {
			_E("__update_task_history_1 ret [%d]", ret);
			break;
		}
	} while (0);

	__destory_dockerd_memory(ctl_docker_p);
	return ret;
}

docker_status_e docker_interface_pull_image(char *image)
{
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	if (strlen(image) == 0)
		return DOCKER_STATUS_INVALID_PARAM;

	ret = __check_dockerd_state();
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_E("Docker Engine error");
		return ret;
	}

	ret = __pull_image(image);
	_D("pull Image State[%d]", ret);

	return ret;
}

docker_status_e docker_interface_create_container(container_create_info_s * create_info_p)
{
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	ret = __check_dockerd_state();
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_E("Docker Engine error");
		return ret;
	}

	ret = __start_containers(create_info_p);
	_D("start service State[%d]", ret);

	return ret;
}

docker_status_e docker_interface_containers_info(containers_info_s * info_p)
{
	dzl_dockerctl_TasksInfo stTasksInfo = { 0, };
	dzl_dockerctl_ServicesInfo stServicesInfo = { 0, };
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;
	int structCnt = 0;
	int index;

	ret = __check_dockerd_state();
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_E("Docker Engine error");
		return ret;
	}

	_D("__get_services_info start");
	ret = __get_services_info(&stServicesInfo);
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_E("__get_services_info error [%d]", ret);
		if (stServicesInfo.pstServices != NULL)
			free(stServicesInfo.pstServices);
		return ret;
	}

	_D("__get_tasks_Info start");
	ret = __get_tasks_Info(&stTasksInfo);
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_E("__get_tasks_Info error [%d]", ret);
		if (stServicesInfo.pstServices != NULL)
			free(stServicesInfo.pstServices);
		if (stTasksInfo.pstTasks != NULL)
			free(stTasksInfo.pstTasks);
		return ret;
	}

	structCnt = stServicesInfo.count;

	// service and tasks structure generated
	if (__copy_ContainersInfo(&stServicesInfo, &stTasksInfo, info_p)) {
		for (index = 0; index < structCnt; index++) {
			if (info_p->container[index].id != NULL)
				free(info_p->container[index].id);
			if (info_p->container[index].name != NULL)
				free(info_p->container[index].name);
			if (info_p->container[index].image_name != NULL)
				free(info_p->container[index].image_name);
			if (info_p->container[index].status != NULL)
				free(info_p->container[index].status);
		}
		if (stServicesInfo.pstServices != NULL)
			free(stServicesInfo.pstServices);
		if (stTasksInfo.pstTasks != NULL)
			free(stTasksInfo.pstTasks);

		return DOCKER_STATUS_UNKNOWN;
	}

	if (stServicesInfo.pstServices != NULL)
		free(stServicesInfo.pstServices);
	if (stTasksInfo.pstTasks != NULL)
		free(stTasksInfo.pstTasks);

	for (int index = 0; index < info_p->count; index++) {
		_D("dzl_dockerctl Container[%d] ID = [%s]", index, info_p->container[index].id);
		_D("dzl_dockerctl Container[%d] Name = [%s]", index, info_p->container[index].name);
		_D("dzl_dockerctl Container[%d] ImageName = [%s]", index, info_p->container[index].image_name);
		_D("dzl_dockerctl Container[%d] Status = [%s]", index, info_p->container[index].status);
	}

	_D("return containerInfo");
	return ret;
}

docker_status_e docker_interface_update_container(container_update_s * update_info_p)
{
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;
	dzl_dockerctl_serviceinfo_s serviceInfo = { 0, };

	if (strlen(update_info_p->container_name) == 0 || strlen(update_info_p->image_name) == 0) {
		_D("Invalid Update Params");
		return DOCKER_STATUS_INVALID_PARAM;
	}

	_D("container[%s]", update_info_p->container_name);
	_D("image[%s]", update_info_p->image_name);

	ret = __check_dockerd_state();
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_D("Docker Engine error");
		return ret;
	}

	ret = __check_service_with_name(update_info_p->container_name, &serviceInfo);
	_D("check service with name [%d]", ret);
	if (ret != DOCKER_STATUS_NO_ERROR) {
		if (serviceInfo.id != NULL)
			free(serviceInfo.id);
		if (serviceInfo.spec_config != NULL)
			free(serviceInfo.spec_config);
		return ret;
	}

	ret = __update_image(&serviceInfo, update_info_p->image_name);

	if (serviceInfo.id != NULL)
		free(serviceInfo.id);
	if (serviceInfo.spec_config != NULL)
		free(serviceInfo.spec_config);
	_D("docker_interface_update_container ret [%d]", ret);

	return ret;
}

container_status_e docker_interface_get_container_state(char *container_name)
{
	container_status_e ret = CONTAINER_STATUS_UNKNOWN;
	dzl_dockerctl_TasksInfo stTasksInfo = { 0, };
	dzl_dockerctl_serviceinfo_s serviceInfo = { 0, };

	if (strlen(container_name) == 0) {
		_D("container name is invalid");
		return CONTAINER_STATUS_UNKNOWN;
	}

	_D("__check_service_with_name start");
	if (__check_service_with_name(container_name, &serviceInfo)) {
		_E("docker_interface_get_container_state error");
		if (serviceInfo.id != NULL)
			free(serviceInfo.id);
		if (serviceInfo.spec_config != NULL)
			free(serviceInfo.spec_config);

		return CONTAINER_STATUS_UNKNOWN;
	}

	_D("__get_tasks_Info start");
	if (__get_tasks_Info(&stTasksInfo)) {
		_E("__get_tasks_Info error");
		if (serviceInfo.id != NULL)
			free(serviceInfo.id);
		if (serviceInfo.spec_config != NULL)
			free(serviceInfo.spec_config);
		if (stTasksInfo.pstTasks != NULL)
			free(stTasksInfo.pstTasks);

		return CONTAINER_STATUS_UNKNOWN;
	}

	_D("__check_status_task_with_name start");
	ret = __check_status_task_with_name(container_name, serviceInfo.id, &stTasksInfo);

	_D("__check_status_task_with_name ret[%d]", ret);

	return ret;
}

docker_status_e docker_interface_get_docker_state(void)
{
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	ret = __check_dockerd_state();

	_D("Docker Engine Status [%d]", ret);

	return ret;
}

///-------- [workaround]
docker_status_e dzl_docker_get_network_status(void)
{
	DOCKER *networks = NULL;
	CURLcode response;
	dzl_dockerctl_NetworksInfo stNetworksInfo = { 0, };
	char retUrl[256] = { 0, };
	int ret = DOCKER_STATUS_NO_ERROR;

	if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_NETWORKS, retUrl, sizeof(retUrl)))
		return DOCKER_STATUS_UNKNOWN;

	networks = dockercomm_docker_init();

	if (networks) {
		response = dockercomm_docker_get(networks, retUrl);
		if (response != CURLE_OK) {
			_D("docker curl response error!!\n");
			dockercomm_docker_destroy(networks);
			return DOCKER_STATUS_ENGINE_API_FAIL;
		}

		if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(networks))) {
			_D("docker_get_network data = %s", dockercomm_docker_buffer(networks));
			ret = dockerform_generate_networksinfo(dockercomm_docker_buffer(networks), &stNetworksInfo);
			if (ret == DOCKER_STATUS_NO_ERROR) {
				_D("services request ok !!! [%d]", ret);
				if (stNetworksInfo.gatewayflag == 0)
					ret = DOCKER_STATUS_NETWORK_BRIDGE_FAIL;

			} else {
				dockercomm_docker_destroy(networks);
				return ret;
			}
		} else {
			_D("network fail !!!");
			_D("%s", dockercomm_docker_buffer(networks));
			ret = DOCKER_STATUS_SERVICE_FAIL;
		}

		dockercomm_docker_destroy(networks);
	} else {
		_D("getServicesInfo alloc fail");
		ret = DOCKER_STATUS_UNKNOWN;
	}

	return ret;
}

int dzl_dockerctl_engine_stop(void)
{
	int pid = 0;
	char command[128] = { 0, };
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

// [workaround] It will be modified after completed seperating engine service
#if 0
	memset(command, 0x00, sizeof(command));
	snprintf(command, strlen("systemctl restart docker-engine") + 1, "systemctl restart docker-engine");
	_D("engine restart command : %s", command);
	ret = __CLI_command(command);
	if (ret != DOCKER_STATUS_NO_ERROR) {
		_D("__CLI_command ret[%d]", ret);
		return ret;
	}
#endif

// [workaround] It will be modified after completed seperating engine service
	// kill docker daemon
	pid = __get_process_pid(DOCKER_DAEMON);
	if (pid == -1) {
		_E("dockerd process is not exist");
		ret = DOCKER_STATUS_ENGINE_FAIL;
	} else {
		memset(command, 0x00, sizeof(command));
		snprintf(command, sizeof(command), "kill -9 %d", pid);
		_D("engine kill command : %s", command);
		ret = __CLI_command(command, 0, NULL);
		if (ret != DOCKER_STATUS_NO_ERROR) {
			_D("__CLI_command ret[%d]", ret);
			return ret;
		}
	}

	return ret;
}

////
//--- Control for life cycle of dockzen including dockerd and swarm.

int dzl_dockerctl_docker_start(void)
{
	int ret = 0;
	char *dockerd_opt_cmd = NULL;

	ret = dzl_setting_get_docker_info(&dockerd_opt_cmd);
	if (dockerd_opt_cmd == NULL) {
		_D("NULL point access: dockerd_opt_cmd is NULL");
		return -1;
	}
	_D("dockerd_opt_cmd(%s)", dockerd_opt_cmd);

	ret = docker_interface_start(dockerd_opt_cmd, dzl_mon_status_monitor_cb);
	_D("docker_interface_start returned, ret = %d", ret);

// [workaround] It will be modified after completed seperating engine service
// Currently, docker daemon initializing is not completed at INIT state
// So, network_check routine is moved to IDLE state
#if 0
	// check network bridge of docker
	ret = dzl_docker_get_network_status();
	_D("dzl_docker_get_network_status returned, ret = %d", ret);
#endif

	if (!dockerd_opt_cmd)
		free(dockerd_opt_cmd);
	return ret;
}

int dzl_dockerctl_set_factory_init_done(int flag)
{
	return dzl_setting_set_factory_init_done(flag);
}

int dzl_dockerctl_get_factory_init_done(void)
{
	return dzl_setting_get_factory_init_done();
}

int __search_image(char *repoTag)
{
	DOCKER *docker;
	CURLcode response;
	char retUrl[256] = { 0, };
	docker_status_e ret = DOCKER_STATUS_NO_ERROR;

	if (strlen(repoTag) > 0) {
		docker = dockercomm_docker_init();
		if (docker) {
			if (dockerform_generate_url(DOCKER_API_VERSION, DOCKER_URL_IMAGES, retUrl, sizeof(retUrl))) {
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_UNKNOWN;
			}

			_D("url[%s], repoTag[%s]", retUrl, repoTag);
			response = dockercomm_docker_get(docker, retUrl);
			if (response != CURLE_OK) {
				_E("docker daemon error!!");
				dockercomm_docker_destroy(docker);
				return DOCKER_STATUS_ENGINE_API_FAIL;
			}

			if (DOCKER_STATUS_NO_ERROR == dockerform_check_response(dockercomm_docker_buffer(docker))) {
				ret = dockerform_generate_image_with_repotag_info(dockercomm_docker_buffer(docker), repoTag);
			} else {
				_E("Engine fail");
				_E("%s", dockercomm_docker_buffer(docker));
				ret = DOCKER_STATUS_ENGINE_API_FAIL;
			}
			dockercomm_docker_destroy(docker);
		} else {
			ret = DOCKER_STATUS_UNKNOWN;
		}
	} else {
		ret = DOCKER_STATUS_INVALID_PARAM;
	}

	return ret;
}

int dzl_dockerctl_system_agent_init(void)
{
	int ret;
	char imageName[256] = { 0 };
	int image_pull_ret = DOCKER_STATUS_UNKNOWN;
	int image_create_ret = DOCKER_STATUS_UNKNOWN;
	container_create_info_s *create_info_p = NULL;

	ret = dzl_setting_get_system_agent_info(&create_info_p);

	if (ret == 0) {
		_D("create_info_s.name(%s)", create_info_p->name);
		_D("create_info_s.image(%s)", create_info_p->image);
		_D("create_info_s.entry_cmd(%s)", create_info_p->entry_cmd);
		_D("create_info_s.volume(%s)", create_info_p->volume);
		_D("create_info_s.port(%s)", create_info_p->port);
		_D("create_info_s.net_mode(%s)", create_info_p->net_mode);
		_D("create_info_s.privileged(%d)", create_info_p->privileged);
		_D("create_info_s.enable(%d)", create_info_p->enable);
		_D("create_info_s.environment(%s)", create_info_p->environment);

		// There is a issue at Docker bridge network initialzing: TPLDOCKER-305, Docker can't start container with network bridge error
		// So, load image have to be enabled after RECOVERY applied: DOCKER_STATUS_NETWORK_BRIDGE_FAIL

		/* v1.8.4 changed, pre_installed files are from wizard tool,
		   creating service of agent and service will be executed from Launcher. */
#if 0
		if (__docker_interface_load_image(imageName) == DOCKER_STATUS_NO_ERROR) {
			image_pull_ret = DOCKER_STATUS_NO_ERROR;
			_D("image load success!!!!!");
			/* agent container need to be remained */
			//remove(imageName);
		} else {
			image_pull_ret = docker_interface_pull_image(create_info_p->image);
			_D("image load failed!!!!!");
		}
#endif

		/* NEED TO CHECK IMAGE IS EXISTED OR NOT */
		if (__search_image(create_info_p->image) == 0) {
			_D("already exist [%s] image!!!", create_info_p->image);
			image_pull_ret = DOCKER_STATUS_NO_ERROR;
		} else
			image_pull_ret = docker_interface_pull_image(create_info_p->image);

		if (image_pull_ret == DOCKER_STATUS_NO_ERROR) {
			image_create_ret = docker_interface_create_container(create_info_p);
			if (image_create_ret != DOCKER_STATUS_NO_ERROR)
				_E("docker_interface_create_container error (%d)", image_create_ret);
		} else
			_E("docker_interface_pull_image error (%d)", image_pull_ret);
	} else {
		_E("docker_setting_get_agent_info failed (%d)", ret);
		_E("there will be no agent container");
	}

	// free create_info_p pointer
	free(create_info_p);
	return image_pull_ret;
}

//--  monitoring function
int dzl_mon_get_containers_info(containers_info_s * containers_info_h)
{
	int ret = 0;
	int index = 0;
	int structCnt = 0;

	_D("dzl_mon_get_containers_info");

	ret = docker_interface_containers_info(containers_info_h);

	if (!ret) {
		structCnt = containers_info_h->count;
		_D("containers_info_h->count = %d", containers_info_h->count);
		for (index = 0; index < structCnt; index++) {
			_D("container[%d] id = %s", index, containers_info_h->container[index].id);
			_D("container[%d] name = %s", index, containers_info_h->container[index].name);
			_D("container[%d] image_name = %s", index, containers_info_h->container[index].image_name);
			_D("container[%d] status = %s", index, containers_info_h->container[index].status);
		}
	} else {
		_D("dzl_mon_get_containers_info ret[%d]", ret);
		return -1;
	}

	return 0;
}

void dzl_mon_status_monitor_cb(status_monitor_type_e status, void *param1, void *param2)
{
	char *container_name = NULL;
	int docker_status = DOCKER_STATUS_UNKNOWN;
	int container_status = CONTAINER_STATUS_UNKNOWN;
	sm_msgq_buf_s msg_buf = { 0, };

	switch (status) {
	case SM_TYPE_DOCKER_STATUS:
		_D("status_monitor_cb: docker-engine status changed");
		docker_status = *(int *)param1;
		msg_buf.sm_type = status;
		msg_buf.docker_status = docker_status;
		_D("docker_status:%d", msg_buf.docker_status);
		__send_message(&msg_buf);
		break;
	case SM_TYPE_CONTAINER_STATUS:
		_D("status_monitor_cb: container status changed");
		container_status = *(int *)param2;
		msg_buf.sm_type = status;
		if (param1 != NULL) {
			container_name = (char *)malloc(sizeof(char) * (strlen(param1) + 1));
			memset(container_name, 0x00, (strlen(param1) + 1));
			strncpy(container_name, param1, strlen(param1) + 1);
			msg_buf.docker_status = DOCKER_STATUS_NO_ERROR;	/* SM_TYPE_CONTAINER_STATUS callback is called when only docker_status is SM_TYPE_CONTAINER_STATUS */
			msg_buf.container_info.container_name = container_name;
			msg_buf.container_info.container_status = container_status;
			_D("container_name:%s", msg_buf.container_info.container_name);
			_D("container_status:%d", msg_buf.container_info.container_status);
		} else {
			/* TO DO: how to notify to docker-interface like this error */
			_E("invalid param, param1(%p)", param1);
		}
		__send_message(&msg_buf);
		break;
	default:
		_W("Not supported SM_TYPE");
		break;
	}
}

//-- update function
int dzl_update_update_container(container_update_s container_update, container_update_res_s * container_update_res)
{
	int ret = 0;
	int index = 0;
	containers_info_s containers_info_h = { 0, };

	_D("dzl_update_update_container");

//  container_update_res = (container_update_res_s *)malloc(sizeof(container_update_res_s));
	container_update_res->container_name = (char *)malloc(sizeof(char) * (strlen(container_update.container_name) + 1));
	strncpy(container_update_res->container_name, container_update.container_name, strlen(container_update.container_name) + 1);
	container_update_res->image_name_new = (char *)malloc(sizeof(char) * (strlen(container_update.image_name) + 1));
	strncpy(container_update_res->image_name_new, container_update.image_name, strlen(container_update.image_name) + 1);

	ret = docker_interface_containers_info(&containers_info_h);
	if (!ret) {
		for (index = 0; index < containers_info_h.count; index++) {
			if (strncmp(container_update_res->container_name, containers_info_h.container[index].name, strlen(containers_info_h.container[index].name)) == 0) {
				container_update_res->image_name_prev = (char *)malloc(sizeof(char) * (strlen(containers_info_h.container[index].image_name) + 1));
				strncpy(container_update_res->image_name_prev, containers_info_h.container[index].image_name, strlen(containers_info_h.container[index].image_name) + 1);
				break;
			}
		}
	}

	_D("container_update.container_name:%s", container_update.container_name);
	_D("container_update.image_name:%s", container_update.image_name);

	ret = docker_interface_update_container(&container_update);
	_D("docker_interface_update_container, return = %d", ret);

	if (ret == 0) {
		container_update_res->status = (char *)malloc(sizeof(char) * (strlen("DOCKZEN_UPDATE_STATE_STARTED") + 1));
		memset(container_update_res->status, 0x00, sizeof(char) * (strlen("DOCKZEN_UPDATE_STATE_STARTED") + 1));
		strncpy(container_update_res->status, "DOCKZEN_UPDATE_STATE_STARTED", strlen("DOCKZEN_UPDATE_STATE_STARTED") + 1);

		_D("container_update_res->container_name : %s", container_update_res->container_name);
		_D("container_update_res->image_name_prev : %s", container_update_res->image_name_prev);
		_D("container_update_res->image_name_new : %s", container_update_res->image_name_new);
		_D("container_update_res->status : %s", container_update_res->status);
	}
	return 0;
}