lcrcontainer.c 53.5 KB
Newer Older
O
overweight 已提交
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
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
 * lcr licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 * Author: wujing
 * Create: 2018-11-08
 * Description: provide container definition
 ******************************************************************************/
/*
 * liblcrapi
 */

#define _GNU_SOURCE

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>

#include <lxc/lxccontainer.h>

#include "constants.h"
#include "error.h"
#include "lcrcontainer.h"
#include "lcrcontainer_execute.h"
#include "lcrcontainer_extend.h"
#include "log.h"
#include "utils.h"
#include "oci_runtime_hooks.h"
#include "oci_runtime_spec.h"
#include "start_generate_config.h"

/*
 * Free lcr_container_info array returned by lcr_list_{active,all}_containers
 */
void lcr_containers_info_free(struct lcr_container_info **info_arr, size_t size)
{
    size_t i = 0;
    struct lcr_container_info *in = NULL;

    if (info_arr == NULL) {
        return;
    }
    if (size == 0) {
        return;
    }
    for (i = 0, in = *info_arr; i < size; i++, in++) {
        free(in->interface);
        free(in->ipv4);
        free(in->ipv6);
        free(in->name);
        free(in->state);
    }
    free(*info_arr);
    *info_arr = NULL;
}

/*
 * Free lcr_container_info returned lcr_container_info_get
 */
void lcr_container_info_free(struct lcr_container_info *info)
{
    if (info == NULL) {
        return;
    }
    free(info->interface);
    info->interface = NULL;
    free(info->ipv4);
    info->ipv4 = NULL;
    free(info->ipv6);
    info->ipv6 = NULL;
    free(info->name);
    info->name = NULL;
    free(info->state);
    info->state = NULL;
    free(info);
}

static inline bool is_container_exists(struct lxc_container *c)
{
    return c->is_defined(c);
}

static inline bool is_container_can_control(struct lxc_container *c)
{
    return c->may_control(c);
}

/*
 * Get one container info for a given name and lcrpath.
 * return struct of container info, or NULL on error.
 */
struct lcr_container_info *lcr_container_info_get(const char *name, const char *lcrpath)
{
    int nret = -1;
    struct lcr_container_info *info = NULL;
    const char *st = NULL;
    bool run_flag = false;

    struct lxc_container *c = lxc_container_without_config_new(name, lcrpath);
    if (c == NULL) {
        return NULL;
    }

    if (!is_container_exists(c)) {
        goto put_and_finish;
    }

    st = c->state(c);
    if (st == NULL) {
        st = "UNKNOWN";
    }
    run_flag = (strcmp(st, "STOPPED") != 0);

    /*Now it makes sense to allocate memory */
    info = util_common_calloc_s(sizeof(*info));
    if (info == NULL) {
        nret = -1;
        goto put_and_finish;
    }
    info->init = -1;
    info->running = run_flag;
    info->name = util_strdup_s(name);
    info->state = util_strdup_s(st);
    if (run_flag) {
        info->init = c->init_pid(c);
    }

    nret = 0;
put_and_finish:
    lxc_container_put(c);
    if (nret != 0) {
        lcr_container_info_free(info);
        info = NULL;
    }
    return info;
}

/*
 * Get a complete list of all containers for a given lcrpath.
 * return Number of containers, or -1 on error.
 **/
int lcr_list_all_containers(const char *lcrpath, struct lcr_container_info **info_arr)
{
    char **container = NULL;
    int n = 0;
    int nret = -1;
    size_t info_size = 0;
    const char *path = lcrpath ? lcrpath : LCRPATH;

    clear_error_message(&g_lcr_error);
    n = list_all_containers(path, &container, NULL);
    if (n == -1) {
        n = 0;
    }

    nret = lcr_containers_info_get(path, info_arr, &info_size, container, n);
    if (info_arr == NULL && nret == 0) {
        return -1;
    } else if (info_arr == NULL || nret == -1) {
        lcr_containers_info_free(info_arr, info_size);
        return -1;
    }

    return (int)info_size;
}

static bool create_container_dir(const struct lxc_container *c)
{
    bool ret = false;
    int nret;
    char *s = NULL;
    mode_t mask = umask(S_IWOTH);
    size_t length = 0;

    if (strlen(c->name) > ((SIZE_MAX - strlen(c->config_path)) - 2)) {
        goto out;
    }

    length = strlen(c->config_path) + strlen(c->name) + 2;
    s = util_common_calloc_s(length);
    if (s == NULL) {
        goto out;
    }

    nret = sprintf_s(s, length, "%s/%s", c->config_path, c->name);
    if (nret < 0) {
        goto out;
    }
    // create container dir
    nret = util_mkdir_p(s, CONFIG_DIRECTORY_MODE);
    if (nret != 0 && errno != EEXIST) {
        SYSERROR("Failed to create container path %s", s);
        goto out;
    }
    ret = true;

out:
    free(s);
    umask(mask);
    return ret;
}

static bool remove_container_dir(const struct lxc_container *c)
{
    char *s = NULL;
    int ret = 0;
    size_t length = 0;

    if (strlen(c->name) > ((SIZE_MAX - strlen(c->config_path)) - 2)) {
        return false;
    }

    length = strlen(c->config_path) + strlen(c->name) + 2;
    s = util_common_calloc_s(length);
    if (s == NULL) {
        return false;
    }

    ret = sprintf_s(s, length, "%s/%s", c->config_path, c->name);
    if (ret < 0) {
        free(s);
        return false;
    }
    ret = util_recursive_rmdir(s, 0);
    free(s);
    if (ret != 0) {
        return false;
    }
    return true;
}

static int lcr_write_file(const char *path, const char *data, size_t len)
{
    char *real_path = NULL;
    int fd = -1;
    int ret = -1;

    if (path == NULL || strlen(path) == 0 || data == NULL || len == 0) {
        return -1;
    }

    if (util_ensure_path(&real_path, path) < 0) {
        ERROR("Failed to ensure path %s", path);
        goto out_free;
    }

    fd = util_open(real_path, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE);
    if (fd == -1) {
        ERROR("Create file %s failed", real_path);
        lcr_set_error_message(LCR_ERR_RUNTIME, "Create file %s failed", real_path);
        goto out_free;
    }

    if (write(fd, data, len) == -1) {
        ERROR("write data to %s failed: %s", real_path, strerror(errno));
        goto out_free;
    }

    ret = 0;

out_free:
    if (fd != -1) {
        close(fd);
    }
    free(real_path);
    return ret;
}

static bool lcr_write_ocihooks(const char *path, const oci_runtime_spec_hooks *hooks)
{
    bool ret = false;
    struct parser_context ctx = { OPT_PARSE_STRICT, stderr };
    parser_error err = NULL;

    char *json_hooks = oci_runtime_spec_hooks_generate_json(hooks, &ctx, &err);
    if (json_hooks == NULL) {
        ERROR("Failed to generate json: %s", err);
        goto out_free;
    }

    if (lcr_write_file(path, json_hooks, strlen(json_hooks)) == -1) {
        ERROR("write json hooks failed: %s", strerror(errno));
        goto out_free;
    }

    ret = true;

out_free:
    free(err);
    free(json_hooks);

    return ret;
}

static bool lcr_save_ocihooks(const char *name, const char *lcrpath, const oci_runtime_spec_hooks *hooks)
{
    const char *path = lcrpath ? lcrpath : LCRPATH;
    char ocihook[PATH_MAX] = { 0 };
    char *bundle = NULL;
    bool bret = false;
    int nret = 0;

    if (name == NULL) {
        ERROR("Missing name");
        return false;
    }

    bundle = lcr_get_bundle(path, name);
    if (bundle == NULL) {
        return false;
    }

    nret = sprintf_s(ocihook, sizeof(ocihook), "%s/%s", bundle, OCIHOOKSFILE);
    if (nret < 0) {
        ERROR("Failed to print string");
        goto out_free;
    }

    bret = lcr_write_ocihooks(ocihook, hooks);

out_free:
    free(bundle);
    return bret;
}

static bool lcr_write_container(const char *path, const oci_runtime_spec *container)
{
    bool ret = false;
    struct parser_context ctx = { OPT_PARSE_STRICT, stderr };
    parser_error err = NULL;

    char *json_container = oci_runtime_spec_generate_json(container, &ctx, &err);
    if (json_container == NULL) {
        ERROR("Failed to generate json: %s", err);
        goto out_free;
    }

    if (lcr_write_file(path, json_container, strlen(json_container)) == -1) {
        ERROR("write json container failed: %s", strerror(errno));
        goto out_free;
    }

    ret = true;

out_free:
    free(err);
    free(json_container);
    return ret;
}

static bool lcr_save_container(const char *name, const char *lcrpath, const oci_runtime_spec *container)
{
    bool bret = false;
    const char *path = lcrpath ? lcrpath : LCRPATH;
    char ociconfig[PATH_MAX] = { 0 };
    char *bundle = NULL;
    int nret = 0;

    if (name == NULL) {
        ERROR("Missing name");
        return bret;
    }

    bundle = lcr_get_bundle(path, name);
    if (bundle == NULL) {
        goto out_free;
    }

    nret = sprintf_s(ociconfig, sizeof(ociconfig), "%s/%s", bundle, OCICONFIGFILE);
    if (nret < 0) {
        ERROR("Failed to print string");
        goto out_free;
    }

    bret = lcr_write_container(ociconfig, container);

out_free:
    free(bundle);
    return bret;
}
/*
 *Expand array for container->mounts
*/
static bool mounts_expand(oci_runtime_spec *container, size_t add_len)
{
    defs_mount **tmp_mount = NULL;
    int ret = 0;
    size_t old_len = container->mounts_len;
    if (add_len >= SIZE_MAX / sizeof(defs_mount *) - old_len) {
        ERROR("Too many mount elements!");
        return false;
    }
    ret = mem_realloc((void **)&tmp_mount, (old_len + add_len) * sizeof(defs_mount *), container->mounts,
                      old_len * sizeof(defs_mount *));
    if (ret == -1) {
        ERROR("memory realloc failed for mount array expand");
        return false;
    }
    container->mounts = tmp_mount;
    container->mounts_len = old_len + add_len;

    return true;
}
/*
 *Get the file path that needs to be mount
*/
static bool mount_get_bundle_file(char **bundle, const char *container_name, const char *lcrpath, const char *filename)
{
    const char *path = lcrpath ? lcrpath : LCRPATH;
    int nret = 0;
    size_t len = 0;

    if (strlen(container_name) > (((SIZE_MAX - strlen(path)) - strlen(filename)) - 3)) {
        return false;
    }

    /* bundle = lcrpath + '/' + container_name + '/' + filename + '\0' */
    len = strlen(path) + strlen(container_name) + strlen(filename) + 3;
    *bundle = util_common_calloc_s(len);
    if (*bundle == NULL) {
        return false;
    }
    nret = sprintf_s(*bundle, len, "%s/%s/%s", path, container_name, filename);
    if (nret < 0) {
        return false;
    }
    return true;
}
/*
 *Mount file
 **/
static bool mount_file(oci_runtime_spec *container, const char *bundle, const char *filename, const char *targetdir)
{
    char dest[PATH_MAX] = { 0 };
    char **options = NULL;
    size_t options_len = 2;
    bool ret = false;
    int nret = 0;
    defs_mount *tmp_mounts = NULL;

    nret = sprintf_s(dest, sizeof(dest), "%s/%s", targetdir, filename);
    if (nret < 0) {
        ERROR("Failed to print string");
        goto out_free;
    }

    if (options_len > (SIZE_MAX / sizeof(char *))) {
        goto out_free;
    }

    options = util_common_calloc_s(options_len * sizeof(char *));
    if (options == NULL) {
        ERROR("Out of memory");
        goto out_free;
    }
    options[0] = util_strdup_s("rbind");
    options[1] = util_strdup_s("rprivate");
    /*generate mount node*/
    tmp_mounts = util_common_calloc_s(sizeof(defs_mount));
    if (tmp_mounts == NULL) {
        ERROR("Malloc tmp_mounts memory failed");
        goto out_free;
    }

    tmp_mounts->destination = util_strdup_s(dest);
    tmp_mounts->source = util_strdup_s(bundle);
    tmp_mounts->type = util_strdup_s("bind");
    tmp_mounts->options = options;
    tmp_mounts->options_len = options_len;
    options = NULL;

    /*expand mount array*/
    if (!mounts_expand(container, 1)) {
        goto out_free;
    }
    /*add a new mount node*/
    container->mounts[container->mounts_len - 1] = tmp_mounts;

    ret = true;
out_free:

    if (!ret) {
        util_free_array(options);
        free_defs_mount(tmp_mounts);
    }
    return ret;
}

/*
 *Mount hostname file to  /etc/hostname
*/
static bool mount_hostname(oci_runtime_spec *container, const struct lxc_container *c)
{
    bool ret = true;
    char *bundle = NULL;
    char *filename = "hostname";
    char *targetdir = "/etc";

    if (container == NULL || container->hostname == NULL) {
        return true;
    }
    /* 1.get file path for hostname */
    ret = mount_get_bundle_file(&bundle, c->name, c->config_path, filename);
    if (!ret) {
        goto out_free;
    }
    /* 2.generate hostname file that need to mount */
    ret = util_write_file(bundle, container->hostname, strlen(container->hostname), true);
    if (!ret) {
        goto out_free;
    }
    /* 3.Add one mount nodes to container->mounts */
    ret = mount_file(container, bundle, filename, targetdir);
    if (!ret) {
        ERROR("mount hostname file failed");
        goto out_free;
    }
out_free:
    free(bundle);
    return ret;
}

/*
 *Mount network file, such as hosts, resolv.conf
*/
static bool mount_network_file(oci_runtime_spec *container, const struct lxc_container *c, const char *full_path,
                               const char *default_str)
{
    bool ret = false;
    char *bundle = NULL;
    char *filename = NULL;
    char *targetdir = NULL;

    if (full_path == NULL) {
        return false;
    }
    targetdir = util_strdup_s(full_path);
    filename = strrchr(targetdir, '/');
    if (filename == NULL) {
        ERROR("Invalid path: %s", targetdir);
        goto out_free;
    }
    *filename = '\0';
    filename += 1;
    // 1. get file path for hosts
    ret = mount_get_bundle_file(&bundle, c->name, c->config_path, filename);
    if (!ret) {
        goto out_free;
    }
    // 2. copy /etc/hosts into container hosts file that need to mount
    if (file_exists(full_path)) {
        ret = util_copy_file(full_path, bundle);
    } else {
        // write default value into bundle
        if (default_str != NULL && strlen(default_str) > 0) {
            ret = util_write_file(bundle, default_str, strlen(default_str), false);
        } else {
            ret = false;
            ERROR("Default value is NULL");
        }
    }
    if (!ret) {
        goto out_free;
    }
    // 3. Add one mount nodes to container->mounts
    ret = mount_file(container, bundle, filename, targetdir);
    if (!ret) {
        ERROR("mount hostname file failed");
        goto out_free;
    }
out_free:
    free(targetdir);
    free(bundle);
    return ret;
}

/*
 *Mount hosts file to  /etc/hosts
*/
static bool mount_hosts(oci_runtime_spec *container, const struct lxc_container *c)
{
    bool ret = false;
    char *bundle = NULL;
    char *content = NULL;
    char *filename = "hosts";
    char *targetdir = "/etc";
    size_t content_len = 0;
    int nret = 0;
    const char *default_config = "127.0.0.1       localhost\n"
                                 "::1     localhost ip6-localhost ip6-loopback\n"
                                 "fe00::0 ip6-localnet\n"
                                 "ff00::0 ip6-mcastprefix\n"
                                 "ff02::1 ip6-allnodes\n"
                                 "ff02::2 ip6-allrouters\n";
    const char *loop_ip = "127.0.0.1    ";

    /* 3.generate content for hosts that need to mount */

    if (strlen(container->hostname) > (((SIZE_MAX - strlen(default_config)) - strlen(loop_ip)) - 2)) {
        goto out_free;
    }

    content_len = strlen(default_config) + strlen(loop_ip) + strlen(container->hostname) + 1 + 1;
    content = util_common_calloc_s(content_len);
    if (content == NULL) {
        ERROR("Memory out");
        goto out_free;
    }

    nret = sprintf_s(content, content_len, "%s%s%s\n", default_config, loop_ip, container->hostname);
    if (nret < 0) {
        ERROR("Sprintf_s failed");
        goto out_free;
    }
    /* 4.get file path for hosts */
    ret = mount_get_bundle_file(&bundle, c->name, c->config_path, filename);
    if (!ret) {
        goto out_free;
    }
    /* 5.generate hosts file that need to mount */
    ret = util_write_file(bundle, content, strlen(content), false);
    if (!ret) {
        goto out_free;
    }
    /* 3.Add one mount nodes to container->mounts */
    ret = mount_file(container, bundle, filename, targetdir);
    if (!ret) {
        ERROR("mount hostname file failed");
        goto out_free;
    }
out_free:
    free(bundle);
    free(content);
    return ret;
}

static bool is_system_container(const oci_runtime_spec *container)
{
    size_t i = 0;
    for (i = 0; container->annotations != NULL && i < container->annotations->len; i++) {
        if (strcmp(container->annotations->keys[i], "system.container") == 0) {
            return true;
        }
    }
    return false;
}

static bool copy_host_file_to_bundle(const struct lxc_container *c, const char *rootfs, const char *filename)
{
    char *bundle = NULL;
    char full_path[PATH_MAX] = { 0 };
    bool ret = true;
    int nret;

    nret = sprintf_s(full_path, sizeof(full_path), "%s%s%s", rootfs, "/etc/", filename);
    if (nret < 0) {
        goto out_free;
    }

    ret = mount_get_bundle_file(&bundle, c->name, c->config_path, filename);
    if (!ret) {
        goto out_free;
    }
    ret = util_copy_file(full_path, bundle);
    if (!ret) {
        goto out_free;
    }

out_free:
    free(bundle);
    return ret;
}

static bool init_system_container_network(const struct lxc_container *c, const char *rootfs)
{
    if (!copy_host_file_to_bundle(c, rootfs, "hostname")) {
        ERROR("Failed to copy hostname from rootfs to container bundle");
        return false;
    }

    if (!copy_host_file_to_bundle(c, rootfs, "hosts")) {
        ERROR("Failed to copy hosts from rootfs to container bundle");
        return false;
    }

    if (!copy_host_file_to_bundle(c, rootfs, "resolv.conf")) {
        ERROR("Failed to copy resolv.conf from rootfs to container bundle");
        return false;
    }

    return true;
}

static inline bool is_mount_destination_hosts(const char *destination)
{
    return strcmp(destination, "/etc/hosts") == 0;
}

static inline bool is_mount_destination_resolv(const char *destination)
{
    return strcmp(destination, "/etc/resolv.conf") == 0;
}

static bool init_network_files_mount(oci_runtime_spec *container, const struct lxc_container *c, bool share_host)
{
    bool ret = false;
    bool has_hosts_mount = false;
    bool has_resolv_mount = false;
    size_t i = 0;

    for (i = 0; i < container->mounts_len; i++) {
        if (is_mount_destination_hosts(container->mounts[i]->destination)) {
            has_hosts_mount = true;
        }
        if (is_mount_destination_resolv(container->mounts[i]->destination)) {
            has_resolv_mount = true;
        }
    }
    ret = true;
    if (!has_resolv_mount) {
        const char *default_ipv4_dns = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4\n";
        // 2. create resolv.conf, hosts files
        if (!mount_network_file(container, c, "/etc/resolv.conf", default_ipv4_dns)) {
            ERROR("Mount /etc/resolv.conf failed");
            return false;
        }
    }
    if (!has_hosts_mount) {
        if (share_host && file_exists("/etc/hosts")) {
            ret = mount_network_file(container, c, "/etc/hosts", NULL);
        } else {
            ret = mount_hosts(container, c);
        }
    }
    if (!ret) {
        ERROR("Mount /etc/hosts failed");
    }
    return ret;
}

static inline bool is_invalid_container(const struct lxc_container *c)
{
    return c == NULL || c->name == NULL || c->config_path == NULL;
}

static inline bool is_annotation_key_net_mode(const char *key)
{
    return strcmp(key, "host.network.mode") == 0;
}

static inline bool is_annotation_value_host(const char *value)
{
    return strcmp(value, "host") == 0;
}

static bool init_network_files(oci_runtime_spec *container, const struct lxc_container *c)
{
    bool share_container = false;
    bool share_host = false;
    size_t i = 0;

    if (is_invalid_container(c)) {
        ERROR("Invalid lxc container");
        return false;
    }
    if (container == NULL || container->hostname == NULL) {
        return true;
    }
    if (is_system_container(container)) {
        return init_system_container_network(c, container->root->path);
    }
    // 1. get network mode
    for (i = 0; container->annotations != NULL && i < container->annotations->len; i++) {
        if (is_annotation_key_net_mode(container->annotations->keys[i])) {
            share_container = strncmp(container->annotations->values[i], "container:", strlen("container:")) == 0;
            share_host = is_annotation_value_host(container->annotations->values[i]);
            break;
        }
    }
    if (share_container) {
        return true;
    }

    return init_network_files_mount(container, c, share_host);
}

static inline bool is_root(const char *path)
{
    return strcmp(path, "/") == 0;
}

static bool create_spec(struct lxc_container *c, const char *dist, const char *container_rootfs)
{
    bool ret = false;
    char config[PATH_MAX] = { 0 };
    int nret = 0;
    struct lcr_list *lcr_conf = NULL;
    char *seccomp_conf = NULL;

    INFO("Create new specification file");
    nret = sprintf_s(config, sizeof(config), "%s/%s/config", c->config_path, c->name);
    if (nret < 0) {
        ERROR("Failed to print string");
        goto err_out;
    }

    lcr_conf = lcr_dist2spec(dist, &seccomp_conf);
    if (lcr_conf == NULL) {
        ERROR("Create distribution specific configuration failed");
        goto err_out;
    }

    if (!lcr_save_spec(c->name, c->config_path, lcr_conf, seccomp_conf)) {
        ERROR("Failed to save configuration");
        goto out_free_conf;
    }

    /* reload the config to update the hwaddr in `config` */
    c->clear_config(c);

    if (!c->load_config(c, config)) {
        ERROR("Failed to load config %s", config);
        goto out_free_conf;
    }

    /* NOTE: update (clear and set) container's utsname
     * will result in two 'lxc.uts.name' in config file due to lxc's problem,
     * But they override each other in the order specified.
     */
    c->clear_config_item(c, "lxc.uts.name");
    c->set_config_item(c, "lxc.uts.name", c->name);

    c->clear_config_item(c, "lxc.rootfs.path");
    if (!is_root(container_rootfs)) {
        c->set_config_item(c, "lxc.rootfs.path", container_rootfs);
    }

    if (!c->save_config(c, config)) {
        ERROR("Save configuration failed\n");
        goto out_free_conf;
    }

    ret = true;

out_free_conf:
    lcr_free_config(lcr_conf);
    free(lcr_conf);

err_out:
    free(seccomp_conf);
    return ret;
}

static int create_partial(const struct lxc_container *c)
{
    size_t len = 0;
    int fd = 0;
    int ret = 0;
    struct flock lk;

    if (strlen(c->name) > ((SIZE_MAX - strlen(c->config_path)) - 10)) {
        return -1;
    }

    // $lxcpath + '/' + $name + '/partial' + \0
    len = strlen(c->config_path) + strlen(c->name) + 10;

    char *path = util_common_calloc_s(len);
    if (path == NULL) {
        ERROR("Out of memory in create_partial");
        return -1;
    }

    ret = sprintf_s(path, len, "%s/%s/partial", c->config_path, c->name);
    if (ret < 0) {
        ERROR("Error writing partial pathname");
        goto out_free;
    }

    fd = util_open(path, O_RDWR | O_CREAT | O_EXCL, DEFAULT_SECURE_FILE_MODE);
    if (fd < 0) {
        SYSERROR("Error creating partial file: %s", path);
        goto out_free;
    }
    lk.l_type = F_WRLCK;
    lk.l_whence = SEEK_SET;
    lk.l_start = 0;
    lk.l_len = 0;
    if (fcntl(fd, F_SETLKW, &lk) < 0) {
        SYSERROR("Error locking partial file %s", path);
        close(fd);
        goto out_free;
    }

    free(path);
    return fd;

out_free:
    free(path);
    return -1;
}

static void remove_partial(const struct lxc_container *c)
{
    size_t len = 0;
    int ret = 0;

    if (strlen(c->name) > ((SIZE_MAX - strlen(c->config_path)) - 10)) {
        return;
    }

    // $lxcpath + '/' + $name + '/partial' + \0
    len = strlen(c->config_path) + strlen(c->name) + 10;

    char *path = util_common_calloc_s(len);
    if (path == NULL) {
        ERROR("Out of memory in remove_partial");
        return;
    }

    ret = sprintf_s(path, len, "%s/%s/partial", c->config_path, c->name);
    if (ret < 0) {
        ERROR("Error writing partial pathname");
        goto out_free;
    }
    if (unlink(path) < 0) {
        SYSERROR("Error unlink partial file %s", path);
    }

out_free:
    free(path);
}

static int prepare_rootfs(struct lxc_container *c, const char *rootfs, char **container_rootfs)
{
    int ret = 0;
    struct stat st;

    if (is_root(rootfs)) {
        DEBUG("Rootfs type: \"/\"");
        *container_rootfs = util_strdup_s("/");
    } else if (strncmp(rootfs, "overlayfs:", 10) == 0) {
        DEBUG("Rootfs type: OverlayFS");
        *container_rootfs = util_strdup_s(rootfs);
    } else {
        ret = stat(rootfs, &st);
        if (ret == 0 && S_ISBLK(st.st_mode)) {
            DEBUG("Rootfs type: block device");
            *container_rootfs = util_strdup_s(rootfs);
        } else if (ret == 0 && S_ISDIR(st.st_mode)) {
            *container_rootfs = util_strdup_s(rootfs);
        } else {
            ERROR("Not supported rootfs type:%s", rootfs);
            ret = 1;
            goto err_out;
        }
    }
err_out:
    return ret;
}

static struct lxc_container *lcr_create_new_container(const char *name, const char *lcrpath)
{
    struct lxc_container *c = NULL;

    c = lxc_container_new(name, lcrpath);
    if (c == NULL) {
        ERROR("Failed to new container.");
        return NULL;
    }

    if (is_container_exists(c)) {
        lxc_container_put(c);
        ERROR("Container already exists.");
        lcr_set_error_message(LCR_ERR_RUNTIME, "Runtime error:Container already exists:%s", name);
        return NULL;
    }

    if (!create_container_dir(c)) {
        lxc_container_put(c);
        return NULL;
    }
    return c;
}

static inline bool is_spec_dist_none(const char *spec_dist)
{
    return strcmp(spec_dist, "none") == 0;
}

static bool lcr_create_spec_dist(struct lxc_container *c, const char *real_rootfs, const char *spec_dist,
                                 const char *oci_config_data)
{
    /* NOTE: a special value can be assigned to `dist`, when it's value
     * is `none`, we will not create default config
     */
    if (!is_spec_dist_none(spec_dist)) {
        if (oci_config_data != NULL) {
            // Translate oci config
            DEBUG("Translate oci config...\n");
            if (!translate_spec(c, oci_config_data, real_rootfs)) {
                return false;
            }
            DEBUG("Translate oci config... done\n");
        } else {
            // Generate default config
            DEBUG("Generate default config...\n");
            if (!create_spec(c, spec_dist, real_rootfs)) {
                return false;
            }
            DEBUG("Generate default config... done\n");
        }
    }
    return true;
}

bool lcr_create(const char *name, const char *lcrpath, const char *rootfs, const char *dist,
                const void *oci_config_data)
{
    struct lxc_container *c = NULL;
    int partial_fd = -1;
    bool bret = false;
    char *real_rootfs = NULL; /*the real rootfs used by the container*/
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;
    const char *spec_dist = dist ? dist : "ubuntu";

    clear_error_message(&g_lcr_error);
    engine_set_log_prefix(name);

    c = lcr_create_new_container(name, tmp_path);
    if (c == NULL) {
        engine_free_log_prefix();
        return false;
    }

    /* Mark that this container is being created */
    partial_fd = create_partial(c);
    if (partial_fd < 0) {
        lxc_container_put(c);
        engine_free_log_prefix();
        return false;
    }

    if (prepare_rootfs(c, rootfs, &real_rootfs)) {
        ERROR("Failed to prepare rootfs");
        goto out_unlock;
    }

    if (!lcr_create_spec_dist(c, real_rootfs, spec_dist, oci_config_data)) {
        goto out_unlock;
    }

    bret = true;
out_unlock:
    free(real_rootfs);
    if (partial_fd >= 0) {
        close(partial_fd);
        remove_partial(c);
    }
    if (!bret) {
        if (!remove_container_dir(c)) {
            WARN("Unable to clean container directory");
        }
        if (!c->destroy(c)) {
            WARN("Unable to clean lxc resources");
        }
    }
    lxc_container_put(c);
    engine_free_log_prefix();
    return bret;
}

static bool lcr_start_check_config(const char *lcrpath, const char *name)
{
    char config[PATH_MAX] = { 0 };
    int nret = 0;

    if (access(lcrpath, O_RDONLY) != 0) {
        ERROR("You lack permission to access %s", lcrpath);
        return false;
    }

    nret = sprintf_s(config, sizeof(config), "%s/%s/config", lcrpath, name);
    if (nret < 0) {
        SYSERROR("Failed to allocated memory");
        return false;
    }

    if (access(config, F_OK) != 0) {
        ERROR("File %s does not exist", config);
        return false;
    }
    return true;
}

static bool wait_start_pid(pid_t pid, int rfd, const char *name, const char *path)
{
    int ret;
    ssize_t size_read = 0;
    char buffer[BUFSIZ] = { 0 };

    ret = wait_for_pid(pid);
    if (ret == 0) {
        return true;
    }

    ERROR("Start container failed\n");
    // set default error
    lcr_set_error_message(LCR_ERR_RUNTIME, "runtime error");

    INFO("begin to stop container\n");
    if (!lcr_kill(name, path, SIGKILL)) {
        ERROR("Failed to stop container");
    }

    size_read = read(rfd, buffer, sizeof(buffer) - 1);
    if (size_read > 0) {
        ERROR("Runtime error: %s", buffer);
        lcr_set_error_message(LCR_ERR_RUNTIME, "runtime error: %s", buffer);
    }
    return false;
}

static int save_container_config_file(const char *rootpath, const char *id, const char *json_data, const char *fname)
{
    int ret = 0;
    int nret = 0;
    char filename[PATH_MAX] = { 0 };
    int fd = -1;
    ssize_t len = 0;

    if (json_data == NULL || strlen(json_data) == 0) {
        goto out;
    }
    nret = sprintf_s(filename, sizeof(filename), "%s/%s/%s", rootpath, id, fname);
    if (nret < 0) {
        ERROR("Failed to print string");
        ret = -1;
        goto out;
    }
    if (file_exists(filename)) {
        goto out;
    }

    fd = util_open(filename, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE);
    if (fd == -1) {
        ERROR("Create file %s failed: %s", filename, strerror(errno));
        ret = -1;
        goto out;
    }

    len = util_write_nointr(fd, json_data, strlen(json_data));
    if (len < 0 || ((size_t)len) != strlen(json_data)) {
        ERROR("Write file %s failed: %s", filename, strerror(errno));
        ret = -1;
    }
    close(fd);
out:
    return ret;
}

#define START_GENERATE_CONFIG "start_generate_config.json"
static int save_start_generate_config_json(const char *rootpath, const struct lcr_start_request *request)
{
    start_generate_config sconf = { 0 };
    char *jsonstr = NULL;
    parser_error jerr = NULL;
    int ret = 0;

    if (request->uid == 0 && request->gid == 0 &&
        (request->additional_gids == NULL || request->additional_gids_len == 0)) {
        return 0;
    }
    sconf.uid = request->uid;
    sconf.gid = request->gid;
    sconf.additional_gids = request->additional_gids;
    sconf.additional_gids_len = request->additional_gids_len;

    jsonstr = start_generate_config_generate_json(&sconf, NULL, &jerr);
    ret = save_container_config_file(rootpath, request->name, jsonstr, START_GENERATE_CONFIG);

    free(jerr);
    free(jsonstr);
    return ret;
}

bool lcr_start(const struct lcr_start_request *request)
{
    int pipefd[2] = { -1, -1 };
    bool ret = false;
    pid_t pid = 0;
    const char *path = NULL;
    if (request == NULL) {
        return false;
    }
    path = request->lcrpath ? request->lcrpath : LCRPATH;

    clear_error_message(&g_lcr_error);
    if (request->name == NULL) {
        ERROR("Missing container name");
        return false;
    }
    engine_set_log_prefix(request->name);

    if (!lcr_start_check_config(path, request->name)) {
        goto out_free;
    }

    if (pipe(pipefd) != 0) {
        ERROR("Failed to create pipe\n");
        goto out_free;
    }
    /* generate start config */
    if (save_start_generate_config_json(path, request) != 0) {
        ERROR("Failed to generate start config json file");
        goto out_free;
    }

    pid = fork();
    if (pid == (pid_t) - 1) {
        ERROR("Failed to fork()\n");
        close(pipefd[0]);
        close(pipefd[1]);
        goto out_free;
    }

    if (pid == (pid_t)0) {
        // child process, dup2 pipefd[1] to stderr
        close(pipefd[0]);
        dup2(pipefd[1], 2);

        execute_lxc_start(request->name, path, request->logpath, request->loglevel, request->daemonize, request->tty,
                          request->open_stdin, request->pidfile, request->console_fifos, request->console_logpath,
                          request->share_ns, request->start_timeout, request->container_pidfile, request->exit_fifo);
    }

    close(pipefd[1]);
    ret = wait_start_pid(pid, pipefd[0], request->name, path);
    close(pipefd[0]);

out_free:
    engine_free_log_prefix();
    return ret;
}

static bool lcr_check_container_running(struct lxc_container *c, const char *name)
{
    if (!is_container_exists(c)) {
        ERROR("No such container");
        lcr_set_error_message(LCR_ERR_RUNTIME, "No such container:%s or the configuration files has been corrupted",
                              name);
        return false;
    }

    if (!is_container_can_control(c)) {
        ERROR("Insufficent privileges to control");
        return false;
    }

    if (!c->is_running(c)) {
        ERROR("Container is not running");
        lcr_set_error_message(LCR_ERR_RUNTIME, "Container is not running:%s", name);
        return false;
    }
    return true;
}

bool lcr_kill(const char *name, const char *lcrpath, uint32_t signal)
{
    struct lxc_container *c = NULL;
    const char *path = lcrpath ? lcrpath : LCRPATH;
    bool ret = false;
    int sret = 0;
    pid_t pid = 0;

    clear_error_message(&g_lcr_error);
    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }

    engine_set_log_prefix(name);
    if (signal >= NSIG) {
        ERROR("'%u' isn't a valid signal number", signal);
        engine_free_log_prefix();
        return false;
    }

    c = lxc_container_new(name, path);
    if (c == NULL) {
        ERROR("Failed to stop container.");
        engine_free_log_prefix();
        return false;
    }

    if (!lcr_check_container_running(c, name)) {
        goto out_put;
    }

    pid = c->init_pid(c);
    if (pid < 0) {
        ERROR("Failed to get init pid");
        goto out_put;
    }

    sret = kill(pid, (int)signal);
    if (sret < 0) {
        if (errno == ESRCH) {
            WARN("Can not kill process (pid=%d) with signal %d for container: no such process", pid, signal);
            ret = true;
            goto out_put;
        }
        ERROR("Can not kill process (pid=%d) with signal %d for container", pid, signal);
        goto out_put;
    }

    ret = true;

out_put:
    lxc_container_put(c);
    engine_free_log_prefix();
    return ret;
}

bool lcr_delete(const char *name, const char *lcrpath)
{
    struct lxc_container *c = NULL;
    const char *path = lcrpath ? lcrpath : LCRPATH;
    bool ret = true;

    clear_error_message(&g_lcr_error);
    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }
    engine_set_log_prefix(name);
    c = lxc_container_new(name, path);
    if (c == NULL) {
        ERROR("Failed to delete container.");
        engine_free_log_prefix();
        return false;
    }

    if (!is_container_can_control(c)) {
        ERROR("Insufficent privileges to control");
        ret = false;
        goto out_put;
    }

    if (!is_container_exists(c)) {
        ERROR("No such container");
        lcr_set_error_message(LCR_ERR_RUNTIME, "No such container:%s or the configuration files has been corrupted",
                              name);
        ret = false;
        (void)c->destroy(c);
        goto out_put;
    }

    if (c->is_running(c)) {
        ERROR("Container %s is running, Stop the container before remove", name);
        lcr_set_error_message(LCR_ERR_RUNTIME, "Container %s is running, Stop the container before remove", name);
        ret = false;
        goto out_put;
    }

    ret = c->destroy(c);
    if (!ret) {
        if (c->error_string != NULL) {
            lcr_set_error_message(LCR_ERR_RUNTIME, "%s", c->error_string);
        }
    }

out_put:
    lxc_container_put(c);
    engine_free_log_prefix();
    return ret;
}

bool lcr_exec(const char *name, const char *lcrpath, int argc, char * const *argv, pid_t *pid)
{
    struct lxc_container *c = NULL;
    const char *path = lcrpath ? lcrpath : LCRPATH;
    int r = 0;
    bool ret = false;
    lxc_attach_options_t options = LXC_ATTACH_OPTIONS_DEFAULT;
    lxc_attach_command_t command = (lxc_attach_command_t) {
        .program = NULL
    };
    if (argc > 0) {
        command.program = argv[0];
        command.argv = (char **)argv;
    }

    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }

    engine_set_log_prefix(name);
    if (geteuid()) {
        if (access(path, O_RDONLY) < 0) {
            ERROR("You lack access to %s", path);
            engine_free_log_prefix();
            return false;
        }
    }

    c = lxc_container_new(name, path);
    if (c == NULL) {
        ERROR("Failed to delete container.");
        engine_free_log_prefix();
        return false;
    }

    if (!lcr_check_container_running(c, name)) {
        goto out_put;
    }

    if (command.program != NULL) {
        r = c->attach(c, lxc_attach_run_command, &command, &options, pid);
    } else {
        r = c->attach(c, lxc_attach_run_shell, NULL, &options, pid);
    }
    if (r < 0) {
        goto out_put;
    }
    ret = true;
out_put:
    lxc_container_put(c);
    engine_free_log_prefix();
    return ret;
}

bool lcr_attach(const char *name, const char *lcrpath, const char *logpath, const char *loglevel,
                const char *console_fifos[], char * const argv[], char * const env[], int64_t timeout,
                pid_t *exec_pid, int *exit_code)
{
    struct lxc_container *c = NULL;
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;
    bool bret = false;

    clear_error_message(&g_lcr_error);

    if (name == NULL) {
        ERROR("Missing container name");
        return bret;
    }

    engine_set_log_prefix(name);

    if (geteuid()) {
        if (access(tmp_path, O_RDONLY) < 0) {
            ERROR("You lack access to %s", tmp_path);
            goto out;
        }
    }

    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failed to delete container.");
        goto out;
    }

    if (!lcr_check_container_running(c, name)) {
        goto out_put;
    }

    lxc_container_put(c);

    /* do attach to wait exit code*/
    bret = do_attach(name, lcrpath, logpath, loglevel, console_fifos, (const char * const *)argv,
                     (const char * const *)env, timeout, exec_pid, exit_code);

    goto out;

out_put:
    lxc_container_put(c);
out:
    engine_free_log_prefix();
    return bret;
}

static bool lcr_check_container_stopped(struct lxc_container *c, const char *name)
{
    if (!is_container_exists(c)) {
        ERROR("No such container");
        lcr_set_error_message(LCR_ERR_RUNTIME, "No such container:%s or the configuration files has been corrupted",
                              name);
        return false;
    }

    if (!is_container_can_control(c)) {
        ERROR("Insufficent privileges to control");
        return false;
    }

    if (c->is_running(c)) {
        ERROR("Container is still running");
        lcr_set_error_message(LCR_ERR_RUNTIME, "Container is still running:%s", name);
        return false;
    }
    return true;
}

bool lcr_clean(const char *name, const char *lcrpath, const char *logpath, const char *loglevel, pid_t pid)
{
    struct lxc_container *c = NULL;
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;
    bool bret = true;

    clear_error_message(&g_lcr_error);

    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }
    engine_set_log_prefix(name);

    if (geteuid()) {
        if (access(tmp_path, O_RDONLY) < 0) {
            ERROR("You lack access to %s", tmp_path);
            engine_free_log_prefix();
            return false;
        }
    }

    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failed to delete container.");
        engine_free_log_prefix();
        return false;
    }

    if (!lcr_check_container_stopped(c, name)) {
        bret = false;
        goto out_put;
    }

    if (!c->clean_container_resource(c, pid)) {
        ERROR("Error: Failed to clean container %s resource\n", name);
        bret = false;
        goto out_put;
    }

out_put:
    lxc_container_put(c);

    engine_free_log_prefix();
    return bret;
}

bool lcr_state(const char *name, const char *lcrpath, struct lcr_container_state *lcs)
{
    struct lxc_container *c = NULL;
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;
    bool bret = true;

    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }
    engine_set_log_prefix(name);
    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failure to retrieve state infomation on %s", tmp_path);
        engine_free_log_prefix();
        return false;
    }

    if (!is_container_exists(c)) {
        ERROR("No such container: %s", name);
        bret = false;
        goto out_put;
    }

    if (!is_container_can_control(c)) {
        ERROR("Insufficent privileges to control");
        bret = false;
        goto out_put;
    }

    do_lcr_state(c, lcs);
out_put:
    lxc_container_put(c);
    engine_free_log_prefix();
    return bret;
}

bool lcr_get_container_pids(const char *name, const char *lcrpath, pid_t **pids, size_t *pids_len)
{
    struct lxc_container *c = NULL;
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;
    bool bret = true;

    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }
    engine_set_log_prefix(name);
    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failure to retrieve state infomation on %s", tmp_path);
        engine_free_log_prefix();
        return false;
    }

    if (!is_container_exists(c)) {
        ERROR("No such container");
        bret = false;
        goto out_put;
    }

    if (!c->get_container_pids(c, pids, pids_len)) {
        ERROR("Error: Failed to get container %s pids\n", name);
        bret = false;
        goto out_put;
    }

out_put:
    lxc_container_put(c);
    engine_free_log_prefix();
    return bret;
}

void lcr_container_state_free(struct lcr_container_state *lcs)
{
    free(lcs->name);
    lcs->name = NULL;
    free(lcs->state);
    lcs->state = NULL;
}

bool lcr_pause(const char *name, const char *lcrpath)
{
    struct lxc_container *c = NULL;
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;
    bool bret = true;

    clear_error_message(&g_lcr_error);

    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }

    engine_set_log_prefix(name);
    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failed to pause container");
        engine_free_log_prefix();
        return false;
    }

    if (!is_container_exists(c)) {
        ERROR("No such container");
        bret = false;
        goto out_put;
    }

    if (!is_container_can_control(c)) {
        ERROR("Insufficent privleges to contol");
        bret = false;
        goto out_put;
    }

    if (!c->freeze(c)) {
        ERROR("Failed to pause");
        bret = false;
        goto out_put;
    }

out_put:
    lxc_container_put(c);
    engine_free_log_prefix();
    return bret;
}

bool lcr_resume(const char *name, const char *lcrpath)
{
    struct lxc_container *c = NULL;
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;
    bool bret = false;

    clear_error_message(&g_lcr_error);
    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }
    engine_set_log_prefix(name);
    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failed to resume container");
        goto out;
    }

    if (!is_container_exists(c)) {
        ERROR("No such container");
        goto out_put;
    }

    if (!is_container_can_control(c)) {
        ERROR("Insufficent privleges to contol");
        goto out_put;
    }

    if (!c->unfreeze(c)) {
        ERROR("Failed to resume");
        goto out_put;
    }

    bret = true;

out_put:
    lxc_container_put(c);
out:
    engine_free_log_prefix();
    return bret;
}

bool lcr_console(const char *name, const char *lcrpath, const char *in_fifo, const char *out_fifo, const char *err_fifo)
{
    struct lxc_container *c = NULL;
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;
    bool bresult = true;

    clear_error_message(&g_lcr_error);
    if (name == NULL) {
        ERROR("Missing container name");
        return false;
    }
    engine_set_log_prefix(name);

    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failed to create container.");
        bresult = false;
        goto out;
    }

    if (!is_container_exists(c)) {
        ERROR("No such container");
        bresult = false;
        goto out_put;
    }

    if (!is_container_can_control(c)) {
        ERROR("Insufficent privleges to contol");
        lcr_set_error_message(LCR_ERR_RUNTIME, "Insufficent privleges to contol");
        bresult = false;
        goto out_put;
    }

    if (!c->is_running(c)) {
        ERROR("It's not running");
        lcr_set_error_message(LCR_ERR_RUNTIME, "You cannot attach to a stopped container, start it first");
        bresult = false;
        goto out_put;
    }

    bresult = c->add_terminal_fifos(c, in_fifo, out_fifo, err_fifo);

out_put:
    lxc_container_put(c);
out:
    engine_free_log_prefix();
    return bresult;
}

static char *lcr_get_config_item(struct lxc_container *c, const char *key, bool running)
{
    char *cret = NULL;
    size_t len = 0;
    int nret = 0;

    if (key == NULL) {
        ERROR("Key cannot be NULL");
        return cret;
    }

    if (running) {
        if (!c->is_running(c)) {
            ERROR("It's not running");
            goto out;
        }
        cret = c->get_running_config_item(c, key);
        goto out;
    }

    nret = c->get_config_item(c, key, NULL, 0);
    if (nret <= 0) {
        ERROR("get config item length failed");
        goto out;
    }

    len = (size_t)(nret);
    if (len > SIZE_MAX / sizeof(char) - 1) {
        ERROR("Config item length is too long!");
        goto out;
    }

    cret = util_common_calloc_s((len + 1) * sizeof(char));
    if (cret == NULL) {
        ERROR("Out of memory");
        goto out;
    }

    if ((size_t)c->get_config_item(c, key, cret, (int)len + 1) != len) {
        free(cret);
        cret = NULL;
    }

out:
    return cret;
}

void lcr_free_console_config(struct lcr_console_config *config)
{
    free(config->log_path);
    config->log_path = NULL;
    free(config->log_file_size);
    config->log_file_size = NULL;
    config->log_rotate = 0;
}

static bool lcr_get_console_config_items(struct lxc_container *c, struct lcr_console_config *config)
{
    bool ret = true;
    char *item = NULL;
    unsigned int trotate = 0;

    config->log_path = lcr_get_config_item(c, "lxc.console.logfile", false);
    if (config->log_path == NULL) {
        DEBUG("Log path is NULL");
    }
    config->log_file_size = lcr_get_config_item(c, "lxc.console.size", false);
    if (config->log_file_size == 0) {
        DEBUG("Log file size is 0");
    }

    item = lcr_get_config_item(c, "lxc.console.rotate", false);
    if (item == NULL) {
        DEBUG("Log rotate is NULL");
    } else {
        if (util_safe_uint(item, &trotate) == 0) {
            config->log_rotate = trotate;
        } else {
            ERROR("trans to uint failed");
            ret = false;
        }
        free(item);
    }
    return ret;
}

bool lcr_get_console_config(const char *name, const char *lcrpath, struct lcr_console_config *config)
{
    bool ret = true;
    struct lxc_container *c = NULL;
    const char *tmp_path = lcrpath ? lcrpath : LCRPATH;

    clear_error_message(&g_lcr_error);
    if (name == NULL || lcrpath == NULL || config == NULL) {
        ERROR("Parameter is NULL");
        return false;
    }
    engine_set_log_prefix(name);
    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failed to create container.");
        engine_free_log_prefix();
        return false;
    }

    if (!is_container_exists(c)) {
        ERROR("No such container");
        lcr_set_error_message(LCR_ERR_RUNTIME, "No such container:%s or the configuration files has been corrupted",
                              name);
        ret = false;
        goto out_put;
    }
    if (!is_container_can_control(c)) {
        ERROR("Insufficent privleges to contol");
        goto out_put;
    }

    ret = lcr_get_console_config_items(c, config);
    if (!ret) {
        lcr_free_console_config(config);
    }

out_put:
    lxc_container_put(c);
    engine_free_log_prefix();
    return ret;
}

bool lcr_update(const char *name, const char *lcrpath, const struct lcr_cgroup_resources *cr)
{
    struct lxc_container *c = NULL;
    bool bret = false;
    const char *tmp_path = NULL;

    clear_error_message(&g_lcr_error);
    if (name == NULL || cr == NULL) {
        ERROR("Invalid input");
        return false;
    }
    engine_set_log_prefix(name);

    tmp_path = lcrpath ? lcrpath : LCRPATH;
    if (access(tmp_path, O_RDONLY) < 0) {
        ERROR("You lack permission to access %s", tmp_path);
        engine_free_log_prefix();
        return false;
    }

    c = lxc_container_new(name, tmp_path);
    if (c == NULL) {
        ERROR("Failed to new container.");
        goto out_free;
    }

    if (!is_container_exists(c)) {
        ERROR("No such container");
        goto out_put;
    }

    if (!is_container_can_control(c)) {
        ERROR("Insufficent privileges to control");
        goto out_put;
    }

    if (c->is_running(c) && cr->kernel_memory_limit) {
        ERROR("Can not update kernel memory to a running container, please stop it first");
        goto out_put;
    }

    if (!do_update(c, name, tmp_path, cr)) {
        goto out_put;
    }

    bret = true;

out_put:
    lxc_container_put(c);

out_free:
    engine_free_log_prefix();
    if (!bret) {
        lcr_try_set_error_message(LCR_ERR_RUNTIME, "Runtime error when updating cgroup");
    }
    return bret;
}

const char *lcr_get_errmsg()
{
    if (g_lcr_error.errcode == LCR_SUCCESS) {
        return errno_to_error_message(LCR_SUCCESS);
    }
    if (g_lcr_error.errcode == LCR_ERR_MEMOUT) {
        return errno_to_error_message(LCR_ERR_MEMOUT);
    }
    if (g_lcr_error.errcode == LCR_ERR_FORMAT) {
        return errno_to_error_message(LCR_ERR_FORMAT);
    }
    return (const char *)g_lcr_error.errmsg;
}

void lcr_free_errmsg()
{
    clear_error_message(&g_lcr_error);
}

int lcr_log_init(const char *name, const char *file, const char *priority, const char *prefix, int quiet,
                 const char *lcrpath)
{
    char *full_path = NULL;
    char *pre_name = "fifo:";
    size_t pre_len = 0;
    struct engine_log_config lconf = { 0 };
    struct lxc_log lxc_log_conf = { 0 };

    pre_len = strlen(pre_name);
    lconf.name = "engine";
    if (file == NULL || strncmp(file, pre_name, pre_len) != 0) {
        lconf.file = NULL;
        lconf.driver = "stdout";
        lconf.priority = priority ? priority : "ERROR";
    } else {
        /*File has prefix "fifo:",*/
        full_path = util_string_split_prefix(pre_len, file);
        lconf.file = full_path;
        lconf.driver = "fifo";
        lconf.priority = priority;
    }
    if (log_enable(&lconf)) {
        fprintf(stderr, "Failed to init log");
        goto out;
    }
    if (full_path != NULL) {
        free(full_path);
    }

    lxc_log_conf.name = name;
    lxc_log_conf.lxcpath = lcrpath;
    lxc_log_conf.file = file;
    lxc_log_conf.level = priority;
    lxc_log_conf.prefix = prefix;
    lxc_log_conf.quiet = quiet > 0 ? true : false;
    return lxc_log_init(&lxc_log_conf);
out:
    free(full_path);
    return -1;
}

bool translate_spec(const struct lxc_container *c, const char *oci_json_data, const char *container_rootfs)
{
    bool ret = false;
    struct lcr_list *lcr_conf = NULL;
    oci_runtime_spec *container = NULL;
    char *seccomp_conf = NULL;

    INFO("Translate new specification file");
    if (!container_parse(NULL, oci_json_data, &container)) {
        goto out_free_conf;
    }

    if (!is_system_container(container)) {
        if (!mount_hostname(container, c)) {
            goto out_free_conf;
        }
    }

    if (!init_network_files(container, c)) {
        goto out_free_conf;
    }

    lcr_conf = lcr_oci2lcr(c, container_rootfs, container, &seccomp_conf);
    if (lcr_conf == NULL) {
        ERROR("Create distribution specific configuration failed");
        goto out_free_conf;
    }

    if (container->hooks != NULL && !lcr_save_ocihooks(c->name, c->config_path, container->hooks)) {
        ERROR("Failed to save %s", OCIHOOKSFILE);
        goto out_free_conf;
    }

    if (!lcr_save_container(c->name, c->config_path, container)) {
        ERROR("Failed to save %s", OCICONFIGFILE);
        goto out_free_conf;
    }

    if (!lcr_save_spec(c->name, c->config_path, lcr_conf, seccomp_conf)) {
        ERROR("Failed to save configuration");
        goto out_free_conf;
    }

    ret = true;

out_free_conf:
    free_oci_runtime_spec(container);

    lcr_free_config(lcr_conf);
    free(lcr_conf);

    free(seccomp_conf);
    return ret;
}