commit b4050921766bcb5c90ba0291c5e696f6939ee6bd Author: overweight <5324761+overweight@user.noreply.gitee.com> Date: Mon Sep 30 10:54:40 2019 -0400 Package init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27ffc2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4460e04 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,74 @@ +cmake_minimum_required (VERSION 3.12.1) +project (lcr) + +option(VERSION "set lcr version" ON) +if (VERSION STREQUAL "ON") + set(LCR_VERSION "1.0.15") +endif() + +option(DEBUG "set lcr gcc option" ON) +if (DEBUG STREQUAL "ON") + add_definitions("-g -o2") +endif() + +option(GCOV "set lcr gcov option" OFF) +if (GCOV STREQUAL "ON") + set(LCR_GCOV "ON") +endif() + +if (LIB_INSTALL_DIR) + set(LIB_INSTALL_DIR_DEFAULT ${LIB_INSTALL_DIR}) +else() + set(LIB_INSTALL_DIR_DEFAULT "lib") +endif() + +# check depends libs and headers +include(cmake/checker.cmake) +if (CHECKER_RESULT) + return() +endif() + +# Get the latest abbreviated commit hash of the working branch +execute_process( + COMMAND git rev-parse HEAD + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_COMMIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE +) +message("-- commit id: " ${GIT_COMMIT_HASH}) + +set(CMAKE_C_FLAGS "-fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror") +set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie -shared -pthread") +set(CMAKE_EXE_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie") + +add_definitions(-DLCRPATH="${CMAKE_INSTALL_PREFIX}/var/lib/lcr") +add_definitions(-DLOGPATH="${CMAKE_INSTALL_PREFIX}/var/log/lcr") +add_definitions(-DLCR_GIT_COMMIT="${GIT_COMMIT_HASH}") + +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/config.h.in" + "${CMAKE_BINARY_DIR}/conf/config.h" +) + +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/lcr.pc.in" + "${CMAKE_BINARY_DIR}/conf/lcr.pc" +) + +# build which type of lcr library +option(USESHARED "set type of liblcr, default is shared" ON) +if (USESHARED STREQUAL "ON") + set(LIBTYPE "SHARED") + message("-- Build shared library") +else () + set(LIBTYPE "STATIC") + message("-- Build static library") +endif() + +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src) + +# install all files +install(FILES ${CMAKE_BINARY_DIR}/conf/lcr.pc + DESTINATION ${LIB_INSTALL_DIR_DEFAULT}/pkgconfig PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE) +install(FILES src/lcrcontainer.h + DESTINATION include/lcr) diff --git a/License/LICENSE b/License/LICENSE new file mode 100644 index 0000000..cefc2d2 --- /dev/null +++ b/License/LICENSE @@ -0,0 +1,121 @@ + 木兰宽松许可证, 第1版 + + 木兰宽松许可证, 第1版 + 2019年8月 http://license.coscl.org.cn/MulanPSL + + 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第1版(“本许可证”)的如下条款的约束: + + 0. 定义 + + “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 + + “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 + + “法人实体”是指提交贡献的机构及其“关联实体”。 + + “关联实体”是指,对“本许可证”下的一方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 + + “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 + + 1. 授予版权许可 + + 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 + + 2. 授予专利许可 + + 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括仅因您或他人修改“贡献”或其他结合而将必然会侵犯到的专利权利要求。如您或您的“关联实体”直接或间接地(包括通过代理、专利被许可人或受让人),就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 + + 3. 无商标许可 + + “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 + + 4. 分发限制 + + 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 + + 5. 免责声明与责任限制 + + “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 + + 条款结束。 + + 如何将木兰宽松许可证,第1版,应用到您的软件 + + 如果您希望将木兰宽松许可证,第1版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: + + 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; + + 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; + + 3, 请将如下声明文本放入每个源文件的头部注释中。 + + Copyright (c) [2019] [name of copyright holder] + [Software Name] is 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. + + + Mulan Permissive Software License,Version 1 + + Mulan Permissive Software License,Version 1 (Mulan PSL v1) + August 2019 http://license.coscl.org.cn/MulanPSL + + Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v1 (this License) with following terms and conditions: + + 0. Definition + + Software means the program and related documents which are comprised of those Contribution and licensed under this License. + + Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. + + Legal Entity means the entity making a Contribution and all its Affiliates. + + Affiliates means entities that control, or are controlled by, or are under common control with a party to this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. + + Contribution means the copyrightable work licensed by a particular Contributor under this License. + + 1. Grant of Copyright License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. + + 2. Grant of Patent License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed, excluding of any patent claims solely be infringed by your or others’ modification or other combinations. If you or your Affiliates directly or indirectly (including through an agent, patent licensee or assignee), institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. + + 3. No Trademark License + + No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in section 4. + + 4. Distribution Restriction + + You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. + + 5. Disclaimer of Warranty and Limitation of Liability + + The Software and Contribution in it are provided without warranties of any kind, either express or implied. In no event shall any Contributor or copyright holder be liable to you for any damages, including, but not limited to any direct, or indirect, special or consequential damages arising from your use or inability to use the Software or the Contribution in it, no matter how it’s caused or based on which legal theory, even if advised of the possibility of such damages. + + End of the Terms and Conditions + + How to apply the Mulan Permissive Software License,Version 1 (Mulan PSL v1) to your software + + To apply the Mulan PSL v1 to your work, for easy identification by recipients, you are suggested to complete following three steps: + + i. Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; + ii. Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package; + iii. Attach the statement to the appropriate annotated syntax at the beginning of each source file. + + Copyright (c) [2019] [name of copyright holder] + [Software Name] is 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..042a592 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# lcr + +`lcr`(Lightweight Container Runtime) is CLI tool for spawning and running containers according to OCI specification. +It is based on `liblxc` and written by `C`. + +## How to Contribute + +We always welcome new contributors. And we are happy to provide guidance for the new contributors. +iSulad follows the kernel coding conventions. You can find a detailed introduction at: + +- https://www.kernel.org/doc/html/v4.10/process/coding-style.html + +## Building + +Without considering distribution specific details a simple + + mkdir -p build && cd ./build && cmake .. && make && sudo make install + +is usually sufficient. + +## Licensing + +lcr is licensed under the Mulan PSL v1. diff --git a/cmake/checker.cmake b/cmake/checker.cmake new file mode 100644 index 0000000..9596dca --- /dev/null +++ b/cmake/checker.cmake @@ -0,0 +1,42 @@ +# check depends library and headers +find_package(PkgConfig REQUIRED) + +macro(_CHECK) +if (${ARGV0} STREQUAL "${ARGV1}") + message("error: can not find " ${ARGV2} " program") + set(CHECKER_RESULT 1) +else() + message("-- found " ${ARGV2} " --- works") +endif() +endmacro() + +#check python +find_program(CMD_PYTHON python) +_CHECK(CMD_PYTHON "CMD_PYTHON-NOTFOUND" "python") + +# check securec +find_path(LIBSECUREC_INCLUDE_DIR securec.h) +_CHECK(LIBSECUREC_INCLUDE_DIR "LIBSECUREC_INCLUDE_DIR-NOTFOUND" "securec.h") + +find_library(LIBSECUREC_LIBRARY securec) +_CHECK(LIBSECUREC_LIBRARY "LIBSECUREC_LIBRARY-NOTFOUND" "libsecurec.so") + +# check liblxc +pkg_check_modules(PC_LIBLXC REQUIRED "lxc>=3") +find_path(LIBLXC_INCLUDE_DIR lxc/lxccontainer.h + HINTS ${PC_LIBLXC_INCLUDEDIR} ${PC_LIBLXC_INCLUDE_DIRS}) +_CHECK(LIBLXC_INCLUDE_DIR "LIBLXC_INCLUDE_DIR-NOTFOUND" "lxc/lxccontainer.h") + +find_library(LIBLXC_LIBRARY lxc + HINTS ${PC_LIBLXC_LIBDIR} ${PC_LIBLXC_LIBRARY_DIRS}) +_CHECK(LIBLXC_LIBRARY "LIBLXC_LIBRARY-NOTFOUND" "liblxc.so") + +# check libyajl +pkg_check_modules(PC_LIBYAJL REQUIRED "yajl>=2") +find_path(LIBYAJL_INCLUDE_DIR yajl/yajl_tree.h + HINTS ${PC_LIBYAJL_INCLUDEDIR} ${PC_LIBYAJL_INCLUDE_DIRS}) +_CHECK(LIBYAJL_INCLUDE_DIR "LIBYAJL_INCLUDE_DIR-NOTFOUND" "yajl/yajl_tree.h") + +find_library(LIBYAJL_LIBRARY yajl + HINTS ${PC_LIBYAJL_LIBDIR} ${PC_LIBYAJL_LIBRARY_DIRS}) +_CHECK(LIBYAJL_LIBRARY "LIBYAJL_LIBRARY-NOTFOUND" "libyajl.so") diff --git a/config.h.in b/config.h.in new file mode 100644 index 0000000..3f3c523 --- /dev/null +++ b/config.h.in @@ -0,0 +1 @@ +#cmakedefine VERSION "@LCR_VERSION@" diff --git a/contrib/oci.config.json b/contrib/oci.config.json new file mode 100644 index 0000000..16f5c26 --- /dev/null +++ b/contrib/oci.config.json @@ -0,0 +1,1672 @@ +{ + "ociVersion": "1.0.0-rc5-dev", + "platform": { + "os": "linux", + "arch": "amd64" + }, + "process": { + "terminal": true, + "consoleSize": { + "height": 0, + "width": 0 + }, + "user": { + "uid": 5, + "gid": 3 + }, + "args": [ + "/bin/bash", + "-x", + "sleep", + "5" + ], + "env": [ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "HOSTNAME=7facfe3313cd", + "TERM=xterm" + ], + "cwd": "/", + "capabilities": [ + "CAP_CHOWN", + "CAP_DAC_OVERRIDE", + "CAP_FSETID", + "CAP_FOWNER", + "CAP_MKNOD", + "CAP_NET_RAW", + "CAP_SETGID", + "CAP_SETUID", + "CAP_SETFCAP", + "CAP_SETPCAP", + "CAP_NET_BIND_SERVICE", + "CAP_SYS_CHROOT", + "CAP_KILL", + "CAP_AUDIT_WRITE" + ], + "rlimits": [ + { + "type": "RLIMIT_NOFILE", + "hard": 1024, + "soft": 1024 + }, + { + "type": "RLIMIT_CPU", + "hard": 124, + "soft": 109994 + } + ], + "apparmorProfile": "docker-default", + "selinuxLabel": "docker-default-selinux" + }, + "root": { + "path": "/var/lib/docker/aufs/mnt/30c8ae0af789e1c313726584699c1ce351e0a4e056885096423235bcbfc6c61a" + }, + "hostname": "7facfe3313cd", + "mounts": [ + { + "destination": "/proc", + "type": "proc", + "source": "proc", + "options": [ + "nosuid", + "noexec", + "nodev" + ] + }, + { + "destination": "/dev", + "type": "tmpfs", + "source": "tmpfs", + "options": [ + "nosuid", + "strictatime", + "mode=755" + ] + }, + { + "destination": "/dev/pts", + "type": "devpts", + "source": "devpts", + "options": [ + "nosuid", + "noexec", + "newinstance", + "ptmxmode=0666", + "mode=0620", + "gid=5" + ] + }, + { + "destination": "/sys", + "type": "sysfs", + "source": "sysfs", + "options": [ + "nosuid", + "noexec", + "nodev", + "ro" + ] + }, + { + "destination": "/sys/fs/cgroup", + "type": "cgroup", + "source": "cgroup", + "options": [ + "ro", + "nosuid", + "noexec", + "nodev" + ] + }, + { + "destination": "/dev/mqueue", + "type": "mqueue", + "source": "mqueue", + "options": [ + "nosuid", + "noexec", + "nodev" + ] + }, + { + "destination": "/etc/resolv.conf", + "type": "bind", + "source": "/var/lib/docker/containers/7facfe3313cdca596b13ab6aab063e577f8540f875943e074c08603ee8efb52c/resolv.conf", + "options": [ + "rbind", + "rprivate" + ] + }, + { + "destination": "/etc/hostname", + "type": "bind", + "source": "/var/lib/docker/containers/7facfe3313cdca596b13ab6aab063e577f8540f875943e074c08603ee8efb52c/hostname", + "options": [ + "rbind", + "rprivate" + ] + }, + { + "destination": "/etc/hosts", + "type": "bind", + "source": "/var/lib/docker/containers/7facfe3313cdca596b13ab6aab063e577f8540f875943e074c08603ee8efb52c/hosts", + "options": [ + "rbind", + "rprivate" + ] + }, + { + "destination": "/dev/shm", + "type": "bind", + "source": "/var/lib/docker/containers/7facfe3313cdca596b13ab6aab063e577f8540f875943e074c08603ee8efb52c/shm", + "options": [ + "rbind", + "rprivate" + ] + } + ], + "hooks": { + "prestart": [ + { + "path": "/usr/bin/dockerd", + "args": [ + "libnetwork-setkey", + "7facfe3313cdca596b13ab6aab063e577f8540f875943e074c08603ee8efb52c", + "007ddd4074870397cf56b5fc958fdcea04314b7bfeaaf8104e13b9c980ed6ac4" + ] + } + ] + }, + "linux": { + "uidMappings": [ + { + "hostID": 1002, + "containerID": 0, + "size": 32002 + } + ], + "gidMappings": [ + { + "hostID": 1001, + "containerID": 0, + "size": 32001 + } + ], + "resources": { + "devices": [ + { + "allow": false, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 5, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 3, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 9, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 8, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 5, + "minor": 0, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 5, + "minor": 1, + "access": "rwm" + }, + { + "allow": false, + "type": "c", + "major": 10, + "minor": 229, + "access": "rwm" + } + ], + "disableOOMKiller": false, + "memory": { + "limit": 536870912, + "reservation": 536870912, + "swap": 536870912, + "kernel": 0, + "kernelTCP": 0, + "swappiness": 20 + }, + "cpu": { + "shares": 1024, + "quota": 1000000, + "period": 500000, + "realtimeRuntime": 950000, + "realtimePeriod": 1000000, + "cpus": "2-3", + "mems": "0-7" + }, + "pids": { + "limit": 32771 + }, + "hugepageLimits": [ + { + "pageSize": "2MB", + "limit": 209715200 + }, + { + "pageSize": "2GB", + "limit": 10971201 + } + ], + "network": { + "classID": 1048577, + "priorities": [ + { + "name": "eth0", + "priority": 500 + }, + { + "name": "eth1", + "priority": 1000 + } + ] + }, + "blockIO": { + "weight": 10, + "leafWeight": 11, + "weightDevice": [ + { + "major": 8, + "minor": 0, + "weight": 500, + "leafWeight": 300 + }, + { + "major": 8, + "minor": 16, + "weight": 500 + } + ], + "throttleReadBpsDevice": [ + { + "major": 8, + "minor": 0, + "rate": 600 + } + ], + "throttleWriteBpsDevice": [ + { + "major": 8, + "minor": 1, + "rate": 601 + } + ], + "throttleReadIOPSDevice": [ + { + "major": 8, + "minor": 0, + "rate": 700 + } + ], + "throttleWriteIOPSDevice": [ + { + "major": 8, + "minor": 1, + "rate": 701 + } + ] + } + }, + "cgroupsPath": "/docker/7facfe3313cdca596b13ab6aab063e577f8540f875943e074c08603ee8efb52c", + "namespaces": [ + { + "type": "mount" + }, + { + "type": "network" + }, + { + "type": "uts" + }, + { + "type": "pid" + }, + { + "type": "ipc" + } + ], + "intelRdt": { + "l3CacheSchema": "L3:0=ffff0;1=3ff" + }, + "sysctl": { + "net.ipv4.ip_forward": "1", + "net.core.somaxconn": "256" + }, + "seccomp": { + "defaultAction": "SCMP_ACT_ERRNO", + "architectures": [ + "SCMP_ARCH_X86_64", + "SCMP_ARCH_X86", + "SCMP_ARCH_X32" + ], + "syscalls": [ + { + "name": "accept", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "accept4", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "access", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "alarm", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "alarm", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "bind", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "brk", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "capget", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "capset", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "chdir", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "chmod", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "chown", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "chown32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "clock_getres", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "clock_gettime", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "clock_nanosleep", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "close", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "connect", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "copy_file_range", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "creat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "dup", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "dup2", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "dup3", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "epoll_create", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "epoll_create1", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "epoll_ctl", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "epoll_ctl_old", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "epoll_pwait", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "epoll_wait", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "epoll_wait_old", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "eventfd", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "eventfd2", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "execve", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "execveat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "exit", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "exit_group", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "faccessat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fadvise64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fadvise64_64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fallocate", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fanotify_mark", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fchdir", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fchmod", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fchmodat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fchown", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fchown32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fchownat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fcntl", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fcntl64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fdatasync", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fgetxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "flistxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "flock", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fork", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fremovexattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fsetxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fstat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fstat64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fstatat64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fstatfs", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fstatfs64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "fsync", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "ftruncate", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "ftruncate64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "futex", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "futimesat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getcpu", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getcwd", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getdents", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getdents64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getegid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getegid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "geteuid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "geteuid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getgid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getgid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getgroups", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getgroups32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getitimer", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getpeername", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getpgid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getpgrp", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getpid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getppid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getpriority", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getrandom", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getresgid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getresgid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getresuid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getresuid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getrlimit", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "get_robust_list", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getrusage", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getsid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getsockname", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getsockopt", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "get_thread_area", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "gettid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "gettimeofday", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getuid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getuid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "getxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "inotify_add_watch", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "inotify_init", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "inotify_init1", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "inotify_rm_watch", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "io_cancel", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "ioctl", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "io_destroy", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "io_getevents", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "ioprio_get", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "ioprio_set", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "io_setup", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "io_submit", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "ipc", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "kill", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "lchown", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "lchown32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "lgetxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "link", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "linkat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "listen", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "listxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "llistxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "_llseek", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "lremovexattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "lseek", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "lsetxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "lstat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "lstat64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "madvise", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "memfd_create", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mincore", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mkdir", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mkdirat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mknod", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mknodat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mlock", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mlock2", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mlockall", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mmap", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mmap2", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mprotect", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mq_getsetattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mq_notify", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mq_open", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mq_timedreceive", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mq_timedsend", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mq_unlink", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "mremap", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "msgctl", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "msgget", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "msgrcv", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "msgsnd", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "msync", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "munlock", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "munlockall", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "munmap", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "nanosleep", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "newfstatat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "_newselect", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "open", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "openat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "pause", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "pipe", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "pipe2", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "poll", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "ppoll", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "prctl", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "pread64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "preadv", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "prlimit64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "pselect6", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "pwrite64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "pwritev", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "read", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "readahead", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "readlink", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "readlinkat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "readv", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "recv", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "recvfrom", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "recvmmsg", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "recvmsg", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "remap_file_pages", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "removexattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rename", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "renameat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "renameat2", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "restart_syscall", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rmdir", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rt_sigaction", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rt_sigpending", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rt_sigprocmask", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rt_sigqueueinfo", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rt_sigreturn", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rt_sigsuspend", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rt_sigtimedwait", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "rt_tgsigqueueinfo", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_getaffinity", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_getattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_getparam", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_get_priority_max", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_get_priority_min", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_getscheduler", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_rr_get_interval", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_setaffinity", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_setattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_setparam", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_setscheduler", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sched_yield", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "seccomp", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "select", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "semctl", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "semget", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "semop", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "semtimedop", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "send", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sendfile", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sendfile64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sendmmsg", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sendmsg", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sendto", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setfsgid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setfsgid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setfsuid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setfsuid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setgid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setgid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setgroups", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setgroups32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setitimer", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setpgid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setpriority", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setregid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setregid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setresgid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setresgid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setresuid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setresuid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setreuid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setreuid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setrlimit", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "set_robust_list", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setsid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setsockopt", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "set_thread_area", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "set_tid_address", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setuid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setuid32", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "setxattr", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "shmat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "shmctl", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "shmdt", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "shmget", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "shutdown", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sigaltstack", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "signalfd", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "signalfd4", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sigreturn", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "socket", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "socketcall", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "socketpair", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "splice", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "stat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "stat64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "statfs", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "statfs64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "symlink", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "symlinkat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sync", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sync_file_range", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "syncfs", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "sysinfo", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "syslog", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "tee", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "tgkill", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "time", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "timer_create", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "timer_delete", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "timerfd_create", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "timerfd_gettime", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "timerfd_settime", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "timer_getoverrun", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "timer_gettime", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "timer_settime", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "times", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "tkill", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "truncate", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "truncate64", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "ugetrlimit", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "umask", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "uname", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "unlink", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "unlinkat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "utime", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "utimensat", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "utimes", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "vfork", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "vmsplice", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "wait4", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "waitid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "waitpid", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "write", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "writev", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "personality", + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "index": 0, + "value": 0, + "valueTwo": 0, + "op": "SCMP_CMP_EQ" + } + ] + }, + { + "name": "personality", + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "index": 0, + "value": 8, + "valueTwo": 0, + "op": "SCMP_CMP_EQ" + } + ] + }, + { + "name": "personality", + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "index": 0, + "value": 4294967295, + "valueTwo": 0, + "op": "SCMP_CMP_EQ" + } + ] + }, + { + "name": "arch_prctl", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "modify_ldt", + "action": "SCMP_ACT_ALLOW" + }, + { + "name": "clone", + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "index": 0, + "value": 2080505856, + "valueTwo": 0, + "op": "SCMP_CMP_MASKED_EQ" + } + ] + }, + { + "name": "chroot", + "action": "SCMP_ACT_ALLOW" + } + ] + }, + "maskedPaths": [ + "/proc/kcore", + "/proc/latency_stats", + "/proc/timer_list", + "/proc/timer_stats", + "/proc/sched_debug", + "/sys/firmware" + ], + "readonlyPaths": [ + "/proc/asound", + "/proc/bus", + "/proc/fs", + "/proc/irq", + "/proc/sys", + "/proc/sysrq-trigger" + ] + } +} diff --git a/lcr.pc.in b/lcr.pc.in new file mode 100644 index 0000000..c667f1e --- /dev/null +++ b/lcr.pc.in @@ -0,0 +1,12 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +libdir=@CMAKE_INSTALL_PREFIX@/lib +localstatedir=@CMAKE_INSTALL_PREFIX@/var +includedir=@CMAKE_INSTALL_PREFIX@/include + +Name: liblcr +Description: light-weighted container runtime library +Version: @LCR_VERSION@ +URL: http://code.huawei.com/containers/liblcr +Libs: -L@CMAKE_INSTALL_PREFIX@/lib -llcr +Cflags: -I@CMAKE_INSTALL_PREFIX@/include + diff --git a/lcr.spec b/lcr.spec new file mode 100644 index 0000000..6538d7f --- /dev/null +++ b/lcr.spec @@ -0,0 +1,78 @@ +%global _version 1.0.15 +%global _release 20190612.105034.gitefd3b7ac +Name: lcr +Version: %{_version} +Release: %{_release}%{?dist} +URL: http://code.huawei.com/containers/lcr +Source: %{name}-1.0.tar.gz +Summary: Lightweight Container Runtime +Group: Applications/System +License: Mulan PSL v1 +BuildRoot: %{_tmppath}/%{name}-%{version} + +BuildRequires: cmake +BuildRequires: lxc +BuildRequires: lxc-devel +BuildRequires: yajl yajl-devel +BuildRequires: libsecurec libsecurec-devel +Requires: rsync bridge-utils lxc +ExclusiveArch: x86_64 aarch64 + +%ifarch x86_64 +Provides: liblcr.so()(64bit) +%endif + +%ifarch aarch64 +Provides: liblcr.so()(64bit) +%endif + +%description +Containers are insulated areas inside a system, which have their own namespace +for filesystem, network, PID, IPC, CPU and memory allocation and which can be +created using the Control Group and Namespace features included in the Linux +kernel. + +This package provides the lightweight container tools and library to control +lxc-based containers. + +%global debug_package %{nil} + +%prep +%setup -c -n %{name}-%{version} + +%build +mkdir -p build +cd build +%cmake -DDEBUG=OFF -DLIB_INSTALL_DIR=%{_libdir} ../ +%make_build + +%install +rm -rf %{buildroot} +cd build +mkdir -p %{buildroot}/{%{_libdir},%{_libdir}/pkgconfig,%{_includedir}/%{name},%{_bindir}} +install -m 0644 ./src/liblcr.so %{buildroot}/%{_libdir}/liblcr.so +install -m 0644 ./conf/lcr.pc %{buildroot}/%{_libdir}/pkgconfig/lcr.pc +install -m 0644 ../src/lcrcontainer.h %{buildroot}/%{_includedir}/%{name}/lcrcontainer.h + +find %{buildroot} -type f -name '*.la' -exec rm -f {} ';' +find %{buildroot} -name '*.a' -exec rm -f {} ';' +find %{buildroot} -name '*.cmake' -exec rm -f {} ';' + +%clean +rm -rf %{buildroot} + +%pre + +%post -p /sbin/ldconfig + +%postun -p /sbin/ldconfig + +%files +%defattr(-,root,root,-) +%{_libdir}/* +%{_libdir}/pkgconfig/%{name}.pc +%{_includedir}/%{name}/lcrcontainer.h + +%changelog +* Fri Apr 14 2017 Hui Wang - 0.0.1 +- Initial RPM release diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..f694f30 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,64 @@ +# generate .c and .h files for json +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/json) + +# get liblcr source files +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} topsrcs) +message("-- Get top srcs: " ${topsrcs}) +aux_source_directory(${CMAKE_BINARY_DIR}/json generatesrcs) +message("-- Get generate srcs: " ${generatesrcs}) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/json/schema/src commonjsonsrcs) +message("-- Get common json srcs: " ${commonjsonsrcs}) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/json topjsonsrcs) +message("-- Get top json srcs: " ${topjsonsrcs}) + +# set liblcr library +add_library(liblcr ${LIBTYPE} ${topsrcs} ${topjsonsrcs} ${commonjsonsrcs} ${generatesrcs}) + +set(check_incs + ${LIBSECUREC_INCLUDE_DIR} + ${LIBLXC_INCLUDE_DIR} + ${LIBYAJL_INCLUDE_DIR} + ) +set(check_libs + ${LIBSECUREC_LIBRARY} + ${LIBLXC_LIBRARY} + ${LIBYAJL_LIBRARY} + ) +list(REMOVE_DUPLICATES check_incs) +list(REMOVE_DUPLICATES check_libs) + +# set liblcr include dirs +target_include_directories(liblcr + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/json + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/json/schema/src + PUBLIC ${CMAKE_BINARY_DIR}/json + PUBLIC ${CMAKE_BINARY_DIR}/conf + PUBLIC ${check_incs} + ) + +# set liblcr compile flags +set_target_properties(liblcr PROPERTIES PREFIX "") + +if (LCR_GCOV) + set(CMAKE_C_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage") + message("------compile with gcov-------------") + message("-----CFLAGS: " ${CMAKE_C_FLAGS_DEBUG}) + message("------------------------------------") +endif() + +target_link_libraries(liblcr ${check_libs}) + +if (CMAKE_TOOLCHAIN_FILE) + target_link_directories(liblcr PUBLIC ${CMAKE_LIBRARY_PATH}) + target_link_libraries(liblcr ${EXTRAL_LINK_LIBS}) +endif() + +add_subdirectory(cmd) +if (LCR_GCOV) + target_link_libraries(liblcr -lgcov) +endif() + +# install all files +install(TARGETS liblcr + LIBRARY DESTINATION ${LIB_INSTALL_DIR_DEFAULT} PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE) diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 0000000..35e00fa --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,239 @@ +/****************************************************************************** + * 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 buffer definition + ******************************************************************************/ +#define _GNU_SOURCE + +#include +#include +#include + +#include "buffer.h" +#include "log.h" +#include "utils.h" + +/* buffer allocate */ +Buffer *buffer_alloc(size_t initial_size) +{ + Buffer *buf = NULL; + char *tmp = NULL; + + if (initial_size == 0) { + return NULL; + } + + buf = util_common_calloc_s(sizeof(Buffer)); + if (buf == NULL) { + return NULL; + } + + tmp = calloc(1, initial_size); + if (tmp == NULL) { + free(buf); + return NULL; + } + + buf->contents = tmp; + buf->bytes_used = 0; + buf->total_size = initial_size; + + return buf; +} + +/* buffer strlen */ +static size_t buffer_strlen(const Buffer *buf) +{ + return buf == NULL ? 0 : buf->bytes_used; +} + +/* buffer free */ +void buffer_free(Buffer *buf) +{ + if (buf == NULL) { + return; + } + free(buf->contents); + buf->contents = NULL; + free(buf); +} + +/* buffer has space */ +static bool buffer_has_space(const Buffer *buf, size_t desired_length) +{ + size_t bytes_remaining = 0; + + if (buf == NULL) { + return false; + } + bytes_remaining = buf->total_size - buf->bytes_used; + + return desired_length <= bytes_remaining; +} + +/* buffer grow */ +static int buffer_grow(Buffer *buf, size_t minimum_size) +{ + size_t factor = 0; + errno_t ret = 0; + size_t new_size = 0; + char *tmp = NULL; + + if (buf == NULL) { + return -1; + } + + factor = buf->total_size; + if (factor < minimum_size) { + factor = minimum_size; + } + + if (factor > SIZE_MAX / 2) { + return -1; + } + + new_size = factor * 2; + if (new_size == 0) { + return -1; + } + + tmp = util_common_calloc_s(new_size); + if (tmp == NULL) { + ERROR("Out of memory"); + return -1; + } + + ret = memcpy_s(tmp, new_size, buf->contents, buf->total_size); + if (ret != EOK) { + ERROR("Failed to copy memory"); + free(tmp); + return -1; + } + + free(buf->contents); + buf->contents = tmp; + buf->total_size = new_size; + + return 0; +} + +/* buffer cat */ +static void buffer_cat(Buffer *buf, const char *append, size_t length) +{ + size_t i = 0; + + if (buf == NULL) { + return; + } + + for (i = 0; i < length; i++) { + if (append[i] == '\0') { + break; + } + + *(buf->contents + buf->bytes_used + i) = append[i]; + } + + buf->bytes_used += i; + *(buf->contents + buf->bytes_used) = '\0'; +} + +/* buffer append */ +static int buffer_append(Buffer *buf, const char *append, size_t length) +{ + size_t desired_length = 0; + + if (buf == NULL) { + return -1; + } + + desired_length = length + 1; + if (!buffer_has_space(buf, desired_length)) { + if (buffer_grow(buf, desired_length) != 0) { + goto error; + } + } + + buffer_cat(buf, append, length); + + return 0; +error: + return -1; +} + +/* buffer nappendf */ +int buffer_nappendf(Buffer *buf, size_t length, const char *format, ...) +{ + int status = 0; + size_t printf_length = 0; + char *tmp = NULL; + va_list argp; + + if (buf == NULL) { + return -1; + } + + if (length > SIZE_MAX / sizeof(char) - 1) { + return -1; + } + printf_length = length + 1; + tmp = calloc(1, printf_length * sizeof(char)); + if (tmp == NULL) { + return -1; + } + + va_start(argp, format); + status = vsprintf_s(tmp, printf_length, format, argp); + va_end(argp); + if (status < 0) { + goto error; + } + + status = buffer_append(buf, tmp, length); + if (status != 0) { + goto error; + } + + free(tmp); + return 0; +error: + free(tmp); + return -1; +} + +/* buffer to string */ +char *buffer_to_s(const Buffer *buf) +{ + size_t len; + char *result = NULL; + + if (buf == NULL) { + return NULL; + } + + len = buffer_strlen(buf); + if (len == SIZE_MAX) { + return NULL; + } + + result = calloc(1, len + 1); + if (result == NULL) { + return NULL; + } + if (strncpy_s(result, len + 1, buf->contents, len) != EOK) { + ERROR("Failed to copy string!"); + free(result); + return NULL; + } + + return result; +} diff --git a/src/buffer.h b/src/buffer.h new file mode 100644 index 0000000..1006bf1 --- /dev/null +++ b/src/buffer.h @@ -0,0 +1,32 @@ +/****************************************************************************** + * 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 buffer definition + ******************************************************************************/ +#ifndef LCRD_BUFFER_H +#define LCRD_BUFFER_H + +#include +#include +#include + +typedef struct Buffer { + char *contents; + size_t bytes_used; + size_t total_size; +} Buffer; + +Buffer *buffer_alloc(size_t initial_size); +void buffer_free(Buffer *buf); +int buffer_nappendf(Buffer *buf, size_t length, const char *format, ...); +char *buffer_to_s(const Buffer *buf); +#endif diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt new file mode 100644 index 0000000..6ca75bf --- /dev/null +++ b/src/cmd/CMakeLists.txt @@ -0,0 +1,22 @@ +# get lcr source files +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} cmdsrcs) + +# set lcr binary +add_executable(lcr ${cmdsrcs}) +target_link_libraries(lcr liblcr) + +# set lcr include headers +target_include_directories(lcr + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} + PUBLIC ${CMAKE_SOURCE_DIR}/src/json + PUBLIC ${CMAKE_SOURCE_DIR}/src/json/schema/src + PUBLIC ${CMAKE_BINARY_DIR}/json + PUBLIC ${CMAKE_BINARY_DIR}/conf + ) +if (LCR_GCOV) + target_link_libraries(lcr -lgcov) +endif() + +# install all files +install(TARGETS lcr + RUNTIME DESTINATION bin PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE) diff --git a/src/cmd/arguments.c b/src/cmd/arguments.c new file mode 100644 index 0000000..2474785 --- /dev/null +++ b/src/cmd/arguments.c @@ -0,0 +1,67 @@ +/****************************************************************************** + * 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 arguments functions + ******************************************************************************/ +#include +#include +#include +#include +#include + +#include "arguments.h" +#include "commands.h" +#include "utils.h" +#include "securec.h" + +/* lcr arguments init */ +void lcr_arguments_init(struct lcr_arguments *args) +{ + args->name = NULL; + args->log_file = NULL; + args->log_priority = NULL; + args->quiet = 0; + args->lcrpath = NULL; + args->create_rootfs = NULL; + args->create_dist = NULL; + args->spec_bundle = NULL; + args->spec_translate = NULL; + args->spec_dist = NULL; + args->list_quiet = false; + args->list_running = false; + args->list_stopped = false; + args->list_active = false; + args->start_daemonize = true; + args->start_pidfile = NULL; + args->console_logpath = NULL; + args->console_fifos[0] = NULL; + args->console_fifos[1] = NULL; + args->delete_force = false; + args->argc = 0; + args->argv = NULL; + args->ociconfig = NULL; +} + + +/* print common help */ +void print_common_help() +{ + size_t len; + struct lcr_arguments cmd_common_args = {}; + struct command_option options[] = { + COMMON_OPTIONS(cmd_common_args) + }; + len = sizeof(options) / sizeof(options[0]); + qsort(options, len, sizeof(options[0]), compare_options); + fprintf(stdout, "COMMON OPTIONS :\n"); + print_options((int)len, options); +} diff --git a/src/cmd/arguments.h b/src/cmd/arguments.h new file mode 100644 index 0000000..98db699 --- /dev/null +++ b/src/cmd/arguments.h @@ -0,0 +1,99 @@ +/****************************************************************************** + * 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 arguments definition + ******************************************************************************/ +#ifndef __LCR_ARGUMENTS_H +#define __LCR_ARGUMENTS_H + +#include +#include +#include + +struct lcr_arguments; + +struct args_cgroup_resources { + char *blkio_weight; + char *cpu_shares; + char *cpu_period; + char *cpu_quota; + char *cpuset_cpus; + char *cpuset_mems; + char *memory_limit; + char *memory_swap; + char *memory_reservation; + char *kernel_memory_limit; +}; + +struct lcr_arguments { + const char *progname; /* sub command name */ + + // For common options + char *name; /*container name */ + char *log_file; + char *log_priority; + int quiet; + + char *lcrpath; + + // lcr create + char *create_rootfs; + char *create_dist; + char *ociconfig; + + // lcr run + // lcr spec + char *spec_bundle; + char *spec_translate; + char *spec_dist; + // lcr list + bool list_quiet; + bool list_active; + bool list_running; + bool list_stopped; + // lcr start + bool start_daemonize; + char *start_pidfile; + char *console_logpath; + const char *console_fifos[2]; + + // lcr kill + char *signal; + + // lcr delete + bool delete_force; + // lcr update + struct args_cgroup_resources cr; + + // remaining arguments + char * const * argv; + int argc; +}; + +#define COMMON_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "logfile", 'o', &(cmdargs).log_file, \ + "Set the log file path wherer debug information is written", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "logpriority", 'l', &(cmdargs).log_priority, "Set log priority to LEVEL", NULL }, \ + { CMD_OPT_TYPE_BOOL, false, "silence", 's', &(cmdargs).quiet, "Don't produce any output to stderr", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "lcrpath", 'P', &(cmdargs).lcrpath, "Use specified container path", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "help", 0, NULL, "Show help", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "version", 0, NULL, "Print the version", NULL } + + +extern void print_common_help(); + +extern void lcr_arguments_init(struct lcr_arguments *args); + +#define lcr_print_error(arg, fmt, args...) \ + fprintf(stderr, "%s: " fmt "\n", (arg)->progname, ## args) + +#endif /*__LCR_ARGUMENTS_H*/ diff --git a/src/cmd/clean.c b/src/cmd/clean.c new file mode 100644 index 0000000..44ab57f --- /dev/null +++ b/src/cmd/clean.c @@ -0,0 +1,71 @@ +/****************************************************************************** + * 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 clean functions + ******************************************************************************/ +#include +#include + +#include "clean.h" +#include "lcrcontainer.h" +#include "arguments.h" +#include "log.h" +#include "utils.h" + +const char g_lcr_cmd_clean_desc[] = + "Delete any resources held by the container often used with detached container NAME"; +static const char g_lcr_cmd_clean_usage[] = "clean [command options] --name=NAME"; + +struct lcr_arguments g_lcr_cmd_clean_args; + +int cmd_clean_main(int argc, const char **argv) +{ + command_t cmd; + struct command_option options[] = { + CLEAN_OPTIONS(g_lcr_cmd_clean_args), + COMMON_OPTIONS(g_lcr_cmd_clean_args) + }; + + lcr_arguments_init(&g_lcr_cmd_clean_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), + argc, (const char **)argv, + g_lcr_cmd_clean_desc, g_lcr_cmd_clean_usage); + if (command_parse_args(&cmd, &(g_lcr_cmd_clean_args.argc), &(g_lcr_cmd_clean_args.argv))) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_clean_args.name, g_lcr_cmd_clean_args.log_file, + g_lcr_cmd_clean_args.log_priority, + g_lcr_cmd_clean_args.progname, + g_lcr_cmd_clean_args.quiet, + LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (g_lcr_cmd_clean_args.name == NULL) { + fprintf(stderr, "missing container name, use -n,--name option\n"); + exit(EXIT_FAILURE); + } + + if (!lcr_clean(g_lcr_cmd_clean_args.name, + g_lcr_cmd_clean_args.lcrpath, + g_lcr_cmd_clean_args.log_file, + g_lcr_cmd_clean_args.log_priority, + 0)) { + fprintf(stderr, "Failed to clean container %s\n", g_lcr_cmd_clean_args.name); + exit(EXIT_FAILURE); + } + + fprintf(stdout, "Container \"%s\" Cleaned\n", g_lcr_cmd_clean_args.name); + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/clean.h b/src/cmd/clean.h new file mode 100644 index 0000000..dee99af --- /dev/null +++ b/src/cmd/clean.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * 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 clean definition + ******************************************************************************/ +#ifndef __CMD_CLEAN_H +#define __CMD_CLEAN_H + +#include "arguments.h" +#include "commander.h" + +#define CLEAN_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL } + +extern const char g_lcr_cmd_clean_desc[]; +extern struct lcr_arguments g_lcr_cmd_clean_args; +int cmd_clean_main(int argc, const char **argv); + +#endif /* __CMD_CLEAN_H */ diff --git a/src/cmd/commander.c b/src/cmd/commander.c new file mode 100644 index 0000000..87f1535 --- /dev/null +++ b/src/cmd/commander.c @@ -0,0 +1,433 @@ +/****************************************************************************** + * 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 commander functions + ******************************************************************************/ +#define _GNU_SOURCE +#include +#include +#include +#include +#include "utils.h" +#include "commander.h" +#include "securec.h" +#include "log.h" + +int compare_options(const void *s1, const void *s2) +{ + return strcmp((*(const command_option_t *)s1).large, (*(const command_option_t *)s2).large); +} + +static int get_max_option_length(int options_len, const command_option_t *options) +{ + int i; + int max_option_len = 0; + + for (i = 0; i < options_len; i++) { + command_option_t option = options[i]; + // -s + int len = 2; + // -s, --large + if (option.large != NULL) { + len = (int)(strlen(option.large) + 6); + } + if (len > max_option_len) { + max_option_len = len; + } + } + + return max_option_len; +} + +static void do_print_options(int options_len, const command_option_t *options, int max_option_len) +{ + int i; + + for (i = 0; i < options_len; i++) { + command_option_t option = options[i]; + int curindex; + int space_left = 0; + + curindex = fprintf(stdout, " "); + if (option.small) { + curindex += fprintf(stdout, "-%c", (char)(option.small)); + } + + if (option.large != NULL) { + if (option.small) { + curindex += fprintf(stdout, ", --%s", option.large); + } else { + curindex += fprintf(stdout, " --%s", option.large); + } + } + + if (curindex <= max_option_len) { + space_left = max_option_len - curindex; + } + + fprintf(stdout, "%*s%s\n", space_left, "", option.description); + } +} + +void print_options(int options_len, const command_option_t *options) +{ + int max_option_len = 0; + + max_option_len = get_max_option_length(options_len, options); + + // format: " -s, --large description" + max_option_len += 6; + + do_print_options(options_len, options, max_option_len); + + fputc('\n', stdout); +} + +void command_help(const command_t *self) +{ + const char *progname = strrchr(self->name, '/'); + if (progname == NULL) { + progname = self->name; + } else { + progname++; + } + + fprintf(stderr, "\nUsage: %s [options] %s\n\n", progname, self->usage); + fprintf(stderr, "%s\n\n", self->description); + qsort(self->options, (size_t)(self->option_count), sizeof(self->options[0]), compare_options); + print_options(self->option_count, self->options); +} + +void command_init(command_t *self, command_option_t *options, int options_len, int argc, const char **argv, + const char *description, const char *usage) +{ + if (memset_s(self, sizeof(command_t), 0, sizeof(command_t)) != EOK) { + COMMAND_ERROR("Failed to set memory"); + return; + } + self->name = argv[0]; + self->argc = argc - 2; + self->argv = argv + 2; + self->usage = usage; + self->description = description; + self->options = options; + self->option_count = options_len; +} + +void command_option(command_t *self, command_option_type_t type, void *data, int small, const char *large, + const char *desc, command_callback_t cb) +{ + if (self->option_count == COMMANDER_MAX_OPTIONS) { + COMMAND_ERROR("Maximum option definitions exceeded\n"); + exit(EINVALIDARGS); + } + int n = self->option_count++; + command_option_t *option = &(self->options[n]); + option->type = type; + option->data = data; + option->cb = cb; + option->small = small; + option->description = desc; + option->large = large; +} + +static int read_option_arg(command_t *self, command_option_t *option, const char **opt_arg, const char **readed) +{ + if ((self == NULL) || (option == NULL) || (opt_arg == NULL)) { + return -1; + } + if (option->hasdata) { + *readed = *opt_arg; + *opt_arg = NULL; + } + if (!option->hasdata && self->argc > 1) { + option->hasdata = true; + *readed = *++(self->argv); + self->argc--; + } + if (!option->hasdata) { + COMMAND_ERROR("Flag needs an argument: --%s", option->large); + return -1; + } + return 0; +} + +static int handle_option_type_bool(const command_option_t *option, const char **opt_arg) +{ + if (option->hasdata && strcmp(*opt_arg, "true") && strcmp(*opt_arg, "false")) { + COMMAND_ERROR("Invalid boolean value \"%s\" for flag --%s", *opt_arg, option->large); + return -1; + } + if (option->hasdata) { + if (!strcmp(*opt_arg, "true")) { + *(bool *)(option->data) = true; + } else { + *(bool *)(option->data) = false; + } + *opt_arg = NULL; + } else { + *(bool *)option->data = true; + } + return 0; +} + +static int handle_option_type_string(command_t *self, command_option_t *option, const char **opt_arg) +{ + if (read_option_arg(self, option, opt_arg, (const char **)(option->data))) { + return -1; + } + if (option->cb != NULL) { + return option->cb(option, *(char **)(option->data)); + } + return 0; +} + +static int handle_option_type_string_dup(command_t *self, command_option_t *option, const char **opt_arg) +{ + const char *readed = NULL; + if (read_option_arg(self, option, opt_arg, &readed)) { + return -1; + } + + free(*(char **)(option->data)); + + *(char **)option->data = util_strdup_s(readed); + + if (option->cb != NULL) { + return option->cb(option, readed); + } + return 0; +} + +static int handle_option_type_callback(command_t *self, command_option_t *option, const char **opt_arg) +{ + const char *readed = NULL; + if (read_option_arg(self, option, opt_arg, &readed)) { + return -1; + } + if (option->cb == NULL) { + COMMAND_ERROR("Must specify callback for type array"); + return -1; + } + return option->cb(option, readed); +} + +static int command_get_option_data(command_t *self, command_option_t *option, const char **opt_arg) +{ + if (option == NULL) { + return -1; + } + switch (option->type) { + case CMD_OPT_TYPE_BOOL: + return handle_option_type_bool(option, opt_arg); + case CMD_OPT_TYPE_STRING: + return handle_option_type_string(self, option, opt_arg); + case CMD_OPT_TYPE_STRING_DUP: + return handle_option_type_string_dup(self, option, opt_arg); + case CMD_OPT_TYPE_CALLBACK: + return handle_option_type_callback(self, option, opt_arg); + default: + COMMAND_ERROR("Unkown command option type:%d", option->type); + return -1; + } +} +static int have_short_options(const command_t *self, char arg) +{ + int i; + for (i = 0; i < self->option_count; i++) { + if (self->options[i].small == arg) { + return 0; + } + } + return -1; +} + +static void set_option_argument_when_match_flag(const char **opt_arg, command_option_t *option, bool *found) +{ + *found = true; + if ((*opt_arg)[1] != '\0') { + if ((*opt_arg)[1] == '=') { + *opt_arg = *opt_arg + 2; + option->hasdata = true; + } else { + *opt_arg = *opt_arg + 1; + } + } else { + *opt_arg = NULL; + } +} + +static int command_parse_short_arg(command_t *self, const char *arg) +{ + const char *opt_arg = arg; + bool found = true; + int j; + + do { + found = false; + if (opt_arg[0] == 'h' && have_short_options(self, 'h') < 0) { + command_help(self); + exit(0); + } + for (j = 0; j < self->option_count; ++j) { + command_option_t *option = &(self->options[j]); + option->hasdata = false; + + if (option->small != opt_arg[0]) { + continue; + } + + // match flag + set_option_argument_when_match_flag(&opt_arg, option, &found); + + if (command_get_option_data(self, option, &opt_arg)) { + return -1; + } + break; + } + } while (found && (opt_arg != NULL)); + + if (opt_arg != NULL) { + COMMAND_ERROR("Unkown flag found:'%c'", opt_arg[0]); + exit(EINVALIDARGS); + } + return 0; +} + +static int command_parse_long_arg(command_t *self, const char *arg) +{ + int j; + + if (!strcmp(arg, "help")) { + command_help(self); + exit(0); + } + + for (j = 0; j < self->option_count; ++j) { + command_option_t *option = &(self->options[j]); + const char *opt_arg = NULL; + option->hasdata = false; + + if (option->large == NULL) { + continue; + } + + opt_arg = str_skip_str(arg, option->large); + if (opt_arg == NULL) { + continue; + } + + if (opt_arg[0]) { + if (opt_arg[0] != '=') { + continue; + } + opt_arg = opt_arg + 1; + option->hasdata = true; + } else { + opt_arg = NULL; + } + if (command_get_option_data(self, option, &opt_arg)) { + return -1; + } + return 0; + } + COMMAND_ERROR("Unkown flag found:'--%s'\n", arg); + exit(EINVALIDARGS); +} + +int command_parse_args(command_t *self, int *argc, char * const **argv) +{ + int ret = 0; + for (; self->argc; self->argc--, self->argv++) { + const char *arg = self->argv[0]; + + if (arg[0] != '-' || !arg[1]) { + break; + } + + // short option + if (arg[1] != '-') { + arg = arg + 1; + ret = command_parse_short_arg(self, arg); + if (!ret) { + continue; + } + break; + } + + // -- + if (!arg[2]) { + self->argc--; + self->argv++; + break; + } + + // long option + arg = arg + 2; + ret = command_parse_long_arg(self, arg); + if (!ret) { + continue; + } + break; + } + if (self->argc > 0) { + *argc = self->argc; + *argv = (char * const *)(self->argv); + } + return ret; +} + +int command_append_array_with_space(command_option_t *option, const char *arg) +{ + int ret = 0; + char *narg = util_string_replace(SPACE_MAGIC_STR, " ", arg); + if (narg == NULL) { + COMMAND_ERROR("Memory allocation error"); + return -1; + } + ret = util_array_append(option->data, narg); + free(narg); + return ret; +} + +int command_append_array(command_option_t *option, const char *arg) +{ + if (option == NULL) { + return -1; + } + char ***array = option->data; + return util_array_append(array, arg); +} + +int command_convert_llong(command_option_t *option, const char *arg) +{ + if (option == NULL) { + return -1; + } + if (util_safe_llong(arg, option->data)) { + COMMAND_ERROR("Invalid value \"%s\" for flag --%s", arg, option->large); + return EINVALIDARGS; + } + return 0; +} + +int command_convert_uint(command_option_t *option, const char *arg) +{ + if (option == NULL) { + return -1; + } + if (util_safe_uint(arg, option->data)) { + COMMAND_ERROR("Invalid value \"%s\" for flag --%s", arg, option->large); + return EINVALIDARGS; + } + return 0; +} diff --git a/src/cmd/commander.h b/src/cmd/commander.h new file mode 100644 index 0000000..6b77b17 --- /dev/null +++ b/src/cmd/commander.h @@ -0,0 +1,84 @@ +/****************************************************************************** + * 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 commander definition + ******************************************************************************/ +#ifndef __COMMANDER_H_ +#define __COMMANDER_H_ +#include +#include +#include + +#ifndef COMMANDER_MAX_OPTIONS +#define COMMANDER_MAX_OPTIONS 64 +#endif + +typedef enum { + /* no arguments */ + CMD_OPT_TYPE_BOOL, + /* required arguments */ + CMD_OPT_TYPE_STRING, + CMD_OPT_TYPE_STRING_DUP, + CMD_OPT_TYPE_CALLBACK +} command_option_type_t; + +struct _command; + +struct command_option; + +typedef int (*command_callback_t)(struct command_option *options, const char *arg); + +typedef struct command_option { + command_option_type_t type; + bool hasdata; + const char *large; + int small; + void *data; + const char *description; + command_callback_t cb; +} command_option_t; + +typedef struct _command { + const char *type; + const char *usage; + const char *description; + const char *name; + const char *version; + int option_count; + command_option_t *options; + int argc; + const char **argv; +} command_t; + +void command_init(command_t *self, command_option_t *options, int options_len, int argc, const char **argv, + const char *description, const char *usage); + +int compare_options(const void *s1, const void *s2); + +void print_options(int options_len, const command_option_t *options); + +void command_help(const command_t *self); + +void command_option(command_t *self, command_option_type_t type, void *data, int small, const char *large, + const char *desc, command_callback_t cb); + +int command_parse_args(command_t *self, int *argc, char * const **argv); + +int command_append_array(command_option_t *option, const char *arg); + +int command_append_array_with_space(command_option_t *option, const char *arg); + +int command_convert_llong(command_option_t *option, const char *arg); + +int command_convert_uint(command_option_t *option, const char *arg); + +#endif /* COMMANDER_H */ diff --git a/src/cmd/commands.c b/src/cmd/commands.c new file mode 100644 index 0000000..6a2cf41 --- /dev/null +++ b/src/cmd/commands.c @@ -0,0 +1,157 @@ +/****************************************************************************** + * 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 commands functions + ******************************************************************************/ +#include +#include + +#include "securec.h" +#include "utils.h" +#include "log.h" +#include "commands.h" +#include "arguments.h" +#include "config.h" // For VERSION + +const char cmd_option_desc[] = "Options"; +const char cmd_option_usage[] = "[OPTIONS] COMMAND [arg...]"; +struct lcr_arguments g_lcr_cmd_args = {}; + +/* print version */ +static void print_version() +{ + printf("Version %s, commit %s\n", VERSION, LCR_GIT_COMMIT); +} + +/*command by name*/ +const struct command *command_by_name(const struct command *commands, const char * const name) +{ + size_t i = 0; + + if (commands == NULL) { + return NULL; + } + + while (1) { + if (!commands[i].name) { + return NULL; + } + + if (strcmp(commands[i].name, name) == 0) { + return commands + i; + } + + ++i; + } +} + +/* compare commands */ +int compare_commands(const void *s1, const void *s2) +{ + return strcmp((*(const struct command *)s1).name, (*(const struct command *)s2).name); +} +// Default help command if implementation doesn't provide one +int command_default_help(const char * const program_name, struct command *commands, int argc, const char **argv) +{ + const struct command *command = NULL; + + if (commands == NULL) { + return 1; + } + + if (argc == 0) { + size_t i = 0; + size_t max_size = 0; + + printf("USAGE:\n"); + printf("\t%s [OPTIONS] COMMAND [args...]\n", program_name); + printf("\n"); + printf("COMMANDS:\n"); + + for (i = 0; commands[i].name != NULL; i++) { + size_t cmd_size = strlen(commands[i].name); + if (cmd_size > max_size) { + max_size = cmd_size; + } + } + qsort(commands, i, sizeof(commands[0]), compare_commands); + for (i = 0; commands[i].name != NULL; i++) { + printf("\t%*s\t%s\n", -(int)max_size, commands[i].name, commands[i].description); + } + + printf("\n"); + print_common_help(); + return 0; + } else if (argc > 1) { + printf("%s: unrecognized argument: \"%s\"\n", program_name, argv[1]); + return 1; + } + + command = command_by_name(commands, argv[0]); + + if (command == NULL) { + printf("%s: sub-command \"%s\" not found\n", program_name, argv[0]); + printf("run `lcr --help` for a list of sub-commonds\n"); + return 1; + } + + if (command->longdesc != NULL) { + printf("%s\n", command->longdesc); + } + return 0; +} + +/* option command init */ +void option_command_init(command_t *self, command_option_t *options, int options_len, int argc, const char **argv, + const char *description, const char *usage) +{ + if (memset_s(self, sizeof(command_t), 0, sizeof(command_t)) != EOK) { + COMMAND_ERROR("Failed to set memory"); + return; + } + self->name = argv[0]; + self->argc = argc - 1; + self->argv = argv + 1; + self->usage = usage; + self->description = description; + self->options = options; + self->option_count = options_len; +} +// Tries to execute a command in the command list. The command name +// must be in argv[1]. Example usage: +// +int run_command(struct command *commands, int argc, const char **argv) +{ + const struct command *command = NULL; + + if (argc == 1) { + return command_default_help(argv[0], commands, argc - 1, argv + 1); + } + + if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { + return command_default_help(argv[0], commands, argc - 2, argv + 2); + } + + if (strcmp(argv[1], "--version") == 0) { + print_version(); + return 0; + } + + command = command_by_name(commands, argv[1]); + if (command != NULL) { + return command->executor(argc, argv); + } + + printf("%s: command \"%s\" not found\n", argv[0], argv[1]); + printf("run `%s --help` or `run -h` for a list of sub-commonds\n", argv[0]); + return 1; +} diff --git a/src/cmd/commands.h b/src/cmd/commands.h new file mode 100644 index 0000000..39ffaa3 --- /dev/null +++ b/src/cmd/commands.h @@ -0,0 +1,49 @@ +/****************************************************************************** + * 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 commands definition + ******************************************************************************/ +#ifndef __COMMAND_H +#define __COMMAND_H + +#include "arguments.h" +#include "commander.h" +#include "utils.h" + +// A command is described by: +// @name: The name which should be passed as a second parameter +// @executor: The function that will be executed if the command +// matches. Receives the argc of the program minus two, and +// the rest os argv +// @description: Brief description, will show in help messages +// @longdesc: Long descripton to show when you run `help ` +struct command { + const char * const name; + int (*executor)(int, const char **); + const char * const description; + const char * const longdesc; + struct lcr_arguments *args; +}; + +// Gets a pointer to a command, to allow implementing custom behavior +// returns null if not found. +// +// NOTE: Command arrays must end in a command with all member is NULL +const struct command *command_by_name(const struct command *commands, const char * const name); + +// Default help command if implementation doesn't prvide one +int commmand_default_help(const char * const program_name, struct command *commands, int argc, char **argv); + +// Tries to execute a command in the command list, The command name +// must be in argv[1]. Example usage +int run_command(struct command *commands, int argc, const char **argv); +#endif /* __COMMAND_H */ diff --git a/src/cmd/create.c b/src/cmd/create.c new file mode 100644 index 0000000..49c8676 --- /dev/null +++ b/src/cmd/create.c @@ -0,0 +1,122 @@ +/****************************************************************************** + * 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 create functions + ******************************************************************************/ +#include +#include + +#include "lcrcontainer.h" +#include "create.h" +#include "arguments.h" +#include "log.h" +#include "utils.h" +#include "read_file.h" + +const char g_lcr_cmd_create_desc[] = "create a container"; +static const char g_lcr_cmd_create_usage[] = "create --name=NAME --rootfs="; + +struct lcr_arguments g_lcr_cmd_create_args = { 0 }; + +static int check_create_args(struct lcr_arguments *lcr_cmd_create_args) +{ + if (lcr_cmd_create_args->name == NULL) { + ERROR("Missing --name,-n option\n"); + return -1; + } + + if (lcr_cmd_create_args->create_rootfs == NULL) { + ERROR("Missing --rootfs option\n"); + return -1; + } + + if (lcr_cmd_create_args->create_dist == NULL) { + lcr_cmd_create_args->create_dist = "ubuntu"; + } + + if (!file_exists(lcr_cmd_create_args->create_rootfs)) { + ERROR("Rootfs dir \"%s\" does not exist\n", lcr_cmd_create_args->create_rootfs); + return -1; + } + + return 0; +} + +static int read_oci_json_data(char **oci_json_data, size_t *filesize) +{ + if (g_lcr_cmd_create_args.ociconfig == NULL) { + return 0; + } + + *oci_json_data = read_file(g_lcr_cmd_create_args.ociconfig, filesize); + if (*oci_json_data == NULL) { + ERROR("Can not read the file \"%s\"\n", g_lcr_cmd_create_args.ociconfig); + return -1; + } + + return 0; +} + +int cmd_create_main(int argc, const char **argv) +{ + char *oci_json_data = NULL; + size_t filesize; + command_t cmd; + struct command_option options[] = { + CREATE_OPTIONS(g_lcr_cmd_create_args), + COMMON_OPTIONS(g_lcr_cmd_create_args) + }; + + lcr_arguments_init(&g_lcr_cmd_create_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, + g_lcr_cmd_create_desc, g_lcr_cmd_create_usage); + + if (command_parse_args(&cmd, &g_lcr_cmd_create_args.argc, &g_lcr_cmd_create_args.argv)) { + exit(EINVALIDARGS); + } + + if (check_create_args(&g_lcr_cmd_create_args)) { + exit(EXIT_FAILURE); + } + + if (read_oci_json_data(&oci_json_data, &filesize) != 0) { + exit(EXIT_FAILURE); + } + + if (lcr_log_init(g_lcr_cmd_create_args.name, + g_lcr_cmd_create_args.log_file, + g_lcr_cmd_create_args.log_priority, + g_lcr_cmd_create_args.progname, + g_lcr_cmd_create_args.quiet, + LOGPATH)) { + free(oci_json_data); + oci_json_data = NULL; + exit(EXIT_FAILURE); + } + + if (!lcr_create(g_lcr_cmd_create_args.name, + g_lcr_cmd_create_args.lcrpath, + g_lcr_cmd_create_args.create_rootfs, + g_lcr_cmd_create_args.create_dist, + oci_json_data)) { + ERROR("Error creating container %s", g_lcr_cmd_create_args.name); + free(oci_json_data); + oci_json_data = NULL; + exit(EXIT_FAILURE); + } + + INFO("Container \"%s\" created\n", g_lcr_cmd_create_args.name); + free(oci_json_data); + oci_json_data = NULL; + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/create.h b/src/cmd/create.h new file mode 100644 index 0000000..eec042f --- /dev/null +++ b/src/cmd/create.h @@ -0,0 +1,36 @@ +/****************************************************************************** + * 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 create definition + ******************************************************************************/ +#ifndef __CMD_CREATE_H +#define __CMD_CREATE_H + +#include "arguments.h" +#include "commander.h" + +#define CREATE_OPTIONS(cmdargs) \ + {CMD_OPT_TYPE_STRING, false, "dist", 0, &(cmdargs).create_dist, \ + "Generate distribution specification, now support: `ubuntu`, `app`," \ + " `none`\n\t\t\tthe default dist is `ubuntu`\n\t\t\tNOTE: if the dist is `none`," \ + " it will not create default spec.", NULL}, \ + {CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL}, \ + {CMD_OPT_TYPE_STRING, false, "ociconfig", 'c', &(cmdargs).ociconfig, \ + "File containing oci configuration (in json format)", NULL}, \ + {CMD_OPT_TYPE_STRING, false, "rootfs", 0, &(cmdargs).create_rootfs, \ + "Specify the rootfs for the container, dir or block device", NULL} + +extern const char g_lcr_cmd_create_desc[]; +extern struct lcr_arguments g_lcr_cmd_create_args; +int cmd_create_main(int argc, const char **argv); + +#endif /* __CMD_CREATE_H */ diff --git a/src/cmd/delete.c b/src/cmd/delete.c new file mode 100644 index 0000000..4d629dc --- /dev/null +++ b/src/cmd/delete.c @@ -0,0 +1,72 @@ +/****************************************************************************** + * 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 delete functions + ******************************************************************************/ +#include +#include + +#include "lcrcontainer.h" +#include "lcrcontainer_extend.h" +#include "delete.h" +#include "arguments.h" +#include "log.h" +#include "utils.h" + +const char g_lcr_cmd_delete_desc[] = "Delete a container"; +static const char g_lcr_cmd_delete_usage[] = "delete --name=NAME"; + +struct lcr_arguments g_lcr_cmd_delete_args; + +static int delete_cmd_init(int argc, const char **argv) +{ + int ret = 0; + command_t cmd; + struct command_option options[] = { DELETE_OPTIONS(g_lcr_cmd_delete_args), COMMON_OPTIONS(g_lcr_cmd_delete_args) }; + + lcr_arguments_init(&g_lcr_cmd_delete_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, g_lcr_cmd_delete_desc, + g_lcr_cmd_delete_usage); + + if (command_parse_args(&cmd, &g_lcr_cmd_delete_args.argc, &g_lcr_cmd_delete_args.argv)) { + ret = -1; + } + + return ret; +} + +int cmd_delete_main(int argc, const char **argv) +{ + if (delete_cmd_init(argc, argv) != 0) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_delete_args.name, g_lcr_cmd_delete_args.log_file, g_lcr_cmd_delete_args.log_priority, + g_lcr_cmd_delete_args.progname, g_lcr_cmd_delete_args.quiet, LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (g_lcr_cmd_delete_args.name == NULL) { + fprintf(stderr, "missing --name,-n option\n"); + exit(EXIT_FAILURE); + } + + if (!lcr_delete_with_force(g_lcr_cmd_delete_args.name, g_lcr_cmd_delete_args.lcrpath, + g_lcr_cmd_delete_args.delete_force)) { + fprintf(stderr, "Error deleteing container %s\n", g_lcr_cmd_delete_args.name); + exit(EXIT_FAILURE); + } + + fprintf(stdout, "Container \"%s\" deleted\n", g_lcr_cmd_delete_args.name); + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/delete.h b/src/cmd/delete.h new file mode 100644 index 0000000..fe27434 --- /dev/null +++ b/src/cmd/delete.h @@ -0,0 +1,30 @@ +/****************************************************************************** + * 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 delete definition + ******************************************************************************/ +#ifndef __CMD_DELETE_H +#define __CMD_DELETE_H + +#include "arguments.h" +#include "commander.h" + +#define DELETE_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_BOOL, false, "force", 'f', &cmdargs.delete_force, \ + "Forcibly deletes the container if it is still running", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "name", 'n', &cmdargs.name, "Name of the container", NULL } + +extern const char g_lcr_cmd_delete_desc[]; +extern struct lcr_arguments g_lcr_cmd_delete_args; +int cmd_delete_main(int argc, const char **argv); + +#endif /* __CMD_DELETE_H */ diff --git a/src/cmd/exec.c b/src/cmd/exec.c new file mode 100644 index 0000000..87c48fc --- /dev/null +++ b/src/cmd/exec.c @@ -0,0 +1,87 @@ +/****************************************************************************** + * 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 exec functions + ******************************************************************************/ +#include +#include + +#include "lcrcontainer.h" +#include "exec.h" +#include "arguments.h" +#include "log.h" +#include "utils.h" + +const char g_lcr_cmd_exec_desc[] = "execute new process inside the container"; +static const char g_lcr_cmd_exec_usage[] = "exec --name=NAME [-- COMMAND]"; + +struct lcr_arguments g_lcr_cmd_exec_args; + +static inline int check_container_name() +{ + if (g_lcr_cmd_exec_args.name == NULL) { + fprintf(stderr, "missing --name,-n option\n"); + return -1; + } + + return 0; +} + +int cmd_exec_main(int argc, const char **argv) +{ + pid_t pid = 0; + int ret; + command_t cmd; + struct command_option options[] = { + EXEC_OPTIONS(g_lcr_cmd_exec_args), + COMMON_OPTIONS(g_lcr_cmd_exec_args) + }; + + lcr_arguments_init(&g_lcr_cmd_exec_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, + g_lcr_cmd_exec_desc, g_lcr_cmd_exec_usage); + if (command_parse_args(&cmd, &g_lcr_cmd_exec_args.argc, &g_lcr_cmd_exec_args.argv)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_exec_args.name, + g_lcr_cmd_exec_args.log_file, + g_lcr_cmd_exec_args.log_priority, + g_lcr_cmd_exec_args.progname, + g_lcr_cmd_exec_args.quiet, + LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (check_container_name() != 0) { + exit(EXIT_FAILURE); + } + + if (!lcr_exec(g_lcr_cmd_exec_args.name, + g_lcr_cmd_exec_args.lcrpath, + g_lcr_cmd_exec_args.argc, + g_lcr_cmd_exec_args.argv, + &pid)) { + fprintf(stderr, "Error execute new process inside container \"%s\"\n", + g_lcr_cmd_exec_args.name); + exit(EXIT_FAILURE); + } + + ret = wait_for_pid(pid) < 0; + if (ret < 0) { + fprintf(stderr, "exec success bug got bad return %d\n", ret); + exit(EXIT_FAILURE); + } + + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/exec.h b/src/cmd/exec.h new file mode 100644 index 0000000..161bd65 --- /dev/null +++ b/src/cmd/exec.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * 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 exec definition + ******************************************************************************/ +#ifndef __CMD_EXEC_H +#define __CMD_EXEC_H + +#include "arguments.h" +#include "commander.h" + +#define EXEC_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL } + +extern const char g_lcr_cmd_exec_desc[]; +extern struct lcr_arguments g_lcr_cmd_exec_args; +int cmd_exec_main(int argc, const char **argv); + +#endif /* __CMD_EXEC_H */ diff --git a/src/cmd/help.c b/src/cmd/help.c new file mode 100644 index 0000000..af0bbea --- /dev/null +++ b/src/cmd/help.c @@ -0,0 +1,33 @@ +/****************************************************************************** + * 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 help functions + ******************************************************************************/ +#include +#include "arguments.h" +#include "help.h" + +const char g_lcr_cmd_help_desc[] = + "show a list of commands or help for one command"; +const char g_lcr_cmd_help_long_desc[] = + "NAME:\n" + "\tlcr help - show a list of commands or help for one command\n" + "\n" + "USAGE:\n" + "\tlcr help [command]\n"; + +/* cmd help main */ +int cmd_help_main(int argc, char **argv, struct lcr_arguments *lcr_cmd_args) +{ + printf("cmd_list\n"); + return 0; +} diff --git a/src/cmd/help.h b/src/cmd/help.h new file mode 100644 index 0000000..32a05df --- /dev/null +++ b/src/cmd/help.h @@ -0,0 +1,25 @@ +/****************************************************************************** + * 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 help definition + ******************************************************************************/ +#ifndef __CMD_HELP_H +#define __CMD_HELP_H + +#include "arguments.h" + +extern const char g_lcr_cmd_help_desc[]; +extern const char g_lcr_cmd_help_long_desc[]; + +int cmd_help_main(int argc, char **argv, struct lcr_arguments *lcr_cmd_args); + +#endif /* __CMD_HELP_H */ diff --git a/src/cmd/kill.c b/src/cmd/kill.c new file mode 100644 index 0000000..8f72794 --- /dev/null +++ b/src/cmd/kill.c @@ -0,0 +1,95 @@ +/****************************************************************************** + * 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 kill functions + ******************************************************************************/ +#include "lcrcontainer.h" + +#include "securec.h" +#include "arguments.h" +#include "kill.h" +#include "log.h" +#include "utils.h" + + +const char g_lcr_cmd_kill_desc[] = "Kill a container with the identifier NAME"; +static const char g_lcr_cmd_kill_usage[] = "kill [command options] --name=NAME"; + +struct lcr_arguments g_lcr_cmd_kill_args; + +static int parse_verify_signal(int *signo) +{ + *signo = util_sig_parse(g_lcr_cmd_kill_args.signal); + if (*signo == -1) { + fprintf(stderr, "Invalid signal: %s", g_lcr_cmd_kill_args.signal); + return -1; + } + + if (!util_valid_signal(*signo)) { + fprintf(stderr, "The Linux daemon does not support signal %d", *signo); + return -1; + } + + return 0; +} + +static inline int check_container_name() +{ + if (g_lcr_cmd_kill_args.name == NULL) { + fprintf(stderr, "Missing container name, use -n,--name option"); + return -1; + } + + return 0; +} +int cmd_kill_main(int argc, const char **argv) +{ + int signo; + command_t cmd; + struct command_option options[] = { + KILL_OPTIONS(g_lcr_cmd_kill_args), + COMMON_OPTIONS(g_lcr_cmd_kill_args) + }; + + lcr_arguments_init(&g_lcr_cmd_kill_args); + g_lcr_cmd_kill_args.signal = "SIGKILL"; + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, + g_lcr_cmd_kill_desc, g_lcr_cmd_kill_usage); + if (command_parse_args(&cmd, &g_lcr_cmd_kill_args.argc, &g_lcr_cmd_kill_args.argv)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_kill_args.name, g_lcr_cmd_kill_args.log_file, + g_lcr_cmd_kill_args.log_priority, + g_lcr_cmd_kill_args.progname, + g_lcr_cmd_kill_args.quiet, + LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (check_container_name() != 0) { + exit(EXIT_FAILURE); + } + + if (parse_verify_signal(&signo) != 0) { + exit(EXIT_FAILURE); + } + + if (!lcr_kill(g_lcr_cmd_kill_args.name, g_lcr_cmd_kill_args.lcrpath, (uint32_t)signo)) { + fprintf(stderr, "Container \"%s\" kill failed", g_lcr_cmd_kill_args.name); + exit(EXIT_FAILURE); + } + + printf("%s\n", g_lcr_cmd_kill_args.name); + + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/kill.h b/src/cmd/kill.h new file mode 100644 index 0000000..4cec54c --- /dev/null +++ b/src/cmd/kill.h @@ -0,0 +1,30 @@ +/****************************************************************************** + * 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 kill definition + ******************************************************************************/ +#ifndef __CMD_KILL_H +#define __CMD_KILL_H + +#include "arguments.h" +#include "commander.h" + +#define KILL_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "signal", 's', &(cmdargs).signal, \ + "Signal to send to the container (default \"SIGKILL\")", NULL } + +extern const char g_lcr_cmd_kill_desc[]; +extern struct lcr_arguments g_lcr_cmd_kill_args; +int cmd_kill_main(int argc, const char **argv); + +#endif /* __CMD_KILL_H */ diff --git a/src/cmd/lcr.c b/src/cmd/lcr.c new file mode 100644 index 0000000..ac48a84 --- /dev/null +++ b/src/cmd/lcr.c @@ -0,0 +1,148 @@ +/****************************************************************************** + * 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 lcr container functions + ******************************************************************************/ +#include +#include +#include + +#include "commands.h" +#include "create.h" +#include "delete.h" +#include "spec.h" +#include "list.h" +#include "start.h" +#include "clean.h" +#include "exec.h" +#include "state.h" +#include "pause.h" +#include "resume.h" +#include "update.h" +#include "help.h" +#include "kill.h" + + +// The list of our supported commands +struct command g_commands[] = { + { + // `create` sub-command + "create", + cmd_create_main, + g_lcr_cmd_create_desc, + NULL, + &g_lcr_cmd_create_args + }, + { + // `delete` sub-command + "delete", + cmd_delete_main, + g_lcr_cmd_delete_desc, + NULL, + &g_lcr_cmd_delete_args + }, + { + // `spec` sub-command + "spec", + cmd_spec_main, + g_lcr_cmd_spec_desc, + NULL, + &g_lcr_cmd_spec_args + }, + { + // `list` sub-command + "list", + cmd_list_main, + g_lcr_cmd_list_desc, + NULL, + &g_lcr_cmd_list_args + }, + { + // `start` sub-command + "start", + cmd_start_main, + g_lcr_cmd_start_desc, + NULL, + &g_lcr_cmd_start_args + }, + { + // `kill` sub-command + "kill", + cmd_kill_main, + g_lcr_cmd_kill_desc, + NULL, + &g_lcr_cmd_kill_args + }, + + { + // `clean` sub-command + "clean", + cmd_clean_main, + g_lcr_cmd_clean_desc, + NULL, + &g_lcr_cmd_clean_args + }, + { + // `exec` sub-command + "exec", + cmd_exec_main, + g_lcr_cmd_exec_desc, + NULL, + &g_lcr_cmd_exec_args + }, + { + // `state` sub-command + "state", + cmd_state_main, + g_lcr_cmd_state_desc, + NULL, + &g_lcr_cmd_state_args + }, + { + // `pause` sub-command + "pause", + cmd_pause_main, + g_lcr_cmd_pause_desc, + NULL, + &g_lcr_cmd_pause_args + }, + { + // `resume` sub-command + "resume", + cmd_resume_main, + g_lcr_cmd_resume_desc, + NULL, + &g_lcr_cmd_resume_args + }, + { + // `update` sub-command + "update", + cmd_update_main, + g_lcr_cmd_update_desc, + NULL, + &g_lcr_cmd_update_args + }, + { + // `help` sub-command + "help", + NULL, + g_lcr_cmd_help_desc, + g_lcr_cmd_help_long_desc, + NULL + }, + { NULL, NULL, NULL, NULL, NULL } // End of the list +}; + +int main(int argc, const char **argv) +{ + return run_command(g_commands, argc, argv); +} diff --git a/src/cmd/list.c b/src/cmd/list.c new file mode 100644 index 0000000..2c90d0e --- /dev/null +++ b/src/cmd/list.c @@ -0,0 +1,222 @@ +/****************************************************************************** + * 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 list functions + ******************************************************************************/ +#include +#include +#include +#include +#include + +#include "lcrcontainer.h" +#include "lcrcontainer_extend.h" + +#include "arguments.h" +#include "list.h" +#include "utils.h" +#include "log.h" + +/* keep track of field widths for printing. */ +struct lcr_lens { + unsigned int lcr_name_len; + unsigned int lcr_state_len; + unsigned int lcr_interface_len; + unsigned int lcr_ipv4_len; + unsigned int lcr_ipv6_len; + unsigned int lcr_init_len; + unsigned int lcr_ram_len; + unsigned int lcr_swap_len; +}; + +const char g_lcr_cmd_list_desc[] = "lists containers"; +static const char g_lcr_cmd_list_usage[] = "list [command options]"; + +static void info_field_width(const struct lcr_container_info *info, const size_t size, struct lcr_lens *l); +static void info_print_table(const struct lcr_container_info *info, size_t size, const struct lcr_lens *length, + const struct lcr_arguments *args); +static void info_print_quiet(const struct lcr_container_info *info, size_t size, const struct lcr_arguments *args); + +struct lcr_arguments g_lcr_cmd_list_args = { 0 }; + +int cmd_list_main(int argc, const char **argv) +{ + command_t cmd; + struct command_option options[] = { LIST_OPTIONS(g_lcr_cmd_list_args), COMMON_OPTIONS(g_lcr_cmd_list_args) }; + + lcr_arguments_init(&g_lcr_cmd_list_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, g_lcr_cmd_list_desc, + g_lcr_cmd_list_usage); + if (command_parse_args(&cmd, &g_lcr_cmd_list_args.argc, &g_lcr_cmd_list_args.argv)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(NULL, g_lcr_cmd_list_args.log_file, g_lcr_cmd_list_args.log_priority, g_lcr_cmd_list_args.progname, + g_lcr_cmd_list_args.quiet, LOGPATH)) { + exit(EXIT_FAILURE); + } + + struct lcr_lens max_len = { + .lcr_name_len = 4, /* NAME */ + .lcr_state_len = 5, /* STATE */ + .lcr_interface_len = 9, /* INTERFACE */ + .lcr_ipv4_len = 4, /* IPV4 */ + .lcr_ipv6_len = 4, /* IPV6 */ + .lcr_init_len = 3, /* PID */ + .lcr_ram_len = 3, /* RAM */ + .lcr_swap_len = 4, /* SWAP */ + }; + + struct lcr_container_info *info_arr = NULL; + int num; + + DEBUG("Get the container information"); + if (g_lcr_cmd_list_args.list_active) { + num = lcr_list_active_containers(g_lcr_cmd_list_args.lcrpath, &info_arr); + } else { + num = lcr_list_all_containers(g_lcr_cmd_list_args.lcrpath, &info_arr); + } + + if (num == -1) { + exit(EXIT_FAILURE); + } + + if (g_lcr_cmd_list_args.list_quiet == true) { + info_print_quiet(info_arr, (size_t)num, &g_lcr_cmd_list_args); + } else { + info_field_width(info_arr, (size_t)num, &max_len); + info_print_table(info_arr, (size_t)num, &max_len, &g_lcr_cmd_list_args); + } + + lcr_containers_info_free(&info_arr, (size_t)num); + exit(EXIT_SUCCESS); +} + +static bool should_skip_print_info(const struct lcr_arguments *args, const struct lcr_container_info *in) +{ + return (args->list_running == true && strncmp(in->state, "RUNNING", 7)) || + (args->list_stopped == true && strncmp(in->state, "STOPPED", 7)); +} + +static void info_print_quiet(const struct lcr_container_info *info, size_t size, const struct lcr_arguments *args) +{ + if (size == 0) { + return; + } + const struct lcr_container_info *in = NULL; + size_t i = 0; + for (i = 0, in = info; i < size; i++, in++) { + if (should_skip_print_info(args, in)) { + continue; + } + printf("%s\n", in->name ? in->name : "-"); + } +} + +static void info_print_table(const struct lcr_container_info *info, size_t size, const struct lcr_lens *length, + const struct lcr_arguments *args) +{ + if (size == 0) { + return; + } + printf("%-*s ", (int)length->lcr_name_len, "NAME"); + printf("%-*s ", (int)length->lcr_state_len, "STATE"); + printf("%-*s ", (int)length->lcr_ipv4_len, "IPV4"); + printf("%-*s ", (int)length->lcr_ipv6_len, "IPV6"); + printf("\n"); + const struct lcr_container_info *in = NULL; + size_t i = 0; + for (i = 0, in = info; i < size; i++, in++) { + if (should_skip_print_info(args, in)) { + continue; + } + printf("%-*s ", (int)length->lcr_name_len, in->name ? in->name : "-"); + printf("%-*s ", (int)length->lcr_state_len, in->state ? in->state : "-"); + printf("%-*s ", (int)length->lcr_ipv4_len, in->ipv4 ? in->ipv4 : "-"); + printf("%-*s ", (int)length->lcr_ipv6_len, in->ipv6 ? in->ipv6 : "-"); + printf("\n"); + } +} + +static void width_ip_and_memory(const struct lcr_container_info *in, struct lcr_lens *l) +{ + size_t len = 0; + char buf[64]; + + if (in->ipv4) { + len = strlen(in->ipv4); + if (len > l->lcr_ipv4_len) { + l->lcr_ipv4_len = (unsigned int)len; + } + } + if (in->ipv6) { + len = strlen(in->ipv6); + if (len > l->lcr_ipv6_len) { + l->lcr_ipv6_len = (unsigned int)len; + } + } + + len = (size_t)sprintf_s(buf, sizeof(buf), "%.2f", in->ram); + if (len > l->lcr_ram_len) { + l->lcr_ram_len = (unsigned int)len; + } + + len = (size_t)sprintf_s(buf, sizeof(buf), "%.2f", in->swap); + if (len > l->lcr_swap_len) { + l->lcr_swap_len = (unsigned int)len; + } +} + +static void set_info_field_max_width(const struct lcr_container_info *in, struct lcr_lens *l) +{ + size_t len; + + if (in->name) { + len = strlen(in->name); + if (len > l->lcr_name_len) { + l->lcr_name_len = (unsigned int)len; + } + } + if (in->state) { + len = strlen(in->state); + if (len > l->lcr_state_len) { + l->lcr_state_len = (unsigned int)len; + } + } + if (in->interface) { + len = strlen(in->interface); + if (len > l->lcr_interface_len) { + l->lcr_interface_len = (unsigned int)len; + } + } + + if (in->init != -1) { + char buf[64]; + int nret = sprintf_s(buf, sizeof(buf), "%d", in->init); + if (nret > 0 && (size_t)nret > l->lcr_init_len) { + l->lcr_init_len = (size_t)nret; + } + } + + width_ip_and_memory(in, l); +} + +static void info_field_width(const struct lcr_container_info *info, const size_t size, struct lcr_lens *l) +{ + size_t i; + const struct lcr_container_info *in = NULL; + + for (i = 0, in = info; i < size; i++, in++) { + set_info_field_max_width(in, l); + } +} diff --git a/src/cmd/list.h b/src/cmd/list.h new file mode 100644 index 0000000..13b2ab2 --- /dev/null +++ b/src/cmd/list.h @@ -0,0 +1,31 @@ +/****************************************************************************** + * 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 list definition + ******************************************************************************/ +#ifndef __CMD_LIST_H +#define __CMD_LIST_H + +#include "arguments.h" +#include "commander.h" + +#define LIST_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_BOOL, false, "quiet", 'q', &(cmdargs).list_quiet, "Display only container names", NULL }, \ + { CMD_OPT_TYPE_BOOL, false, "active", 0, &(cmdargs).list_active, "List only active containers", NULL }, \ + { CMD_OPT_TYPE_BOOL, false, "running", 0, &(cmdargs).list_running, "List only running containers", NULL }, \ + { CMD_OPT_TYPE_BOOL, false, "stopped", 0, &(cmdargs).list_stopped, "List only stopped containers", NULL } + +extern const char g_lcr_cmd_list_desc[]; +extern struct lcr_arguments g_lcr_cmd_list_args; +int cmd_list_main(int argc, const char **argv); + +#endif /* __CMD_LIST_H */ diff --git a/src/cmd/pause.c b/src/cmd/pause.c new file mode 100644 index 0000000..a41408e --- /dev/null +++ b/src/cmd/pause.c @@ -0,0 +1,68 @@ +/****************************************************************************** + * 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 pause functions + ******************************************************************************/ +#include +#include + +#include "lcrcontainer.h" + +#include "arguments.h" +#include "pause.h" +#include "log.h" +#include "utils.h" + +const char g_lcr_cmd_pause_desc[] = "Pause container in specified container NAME"; +static const char g_lcr_cmd_pause_usage[] = "pause [command options] --name=NAME"; + +struct lcr_arguments g_lcr_cmd_pause_args; + +int cmd_pause_main(int argc, const char **argv) +{ + command_t cmd; + struct command_option options[] = { + PAUSE_OPTIONS(g_lcr_cmd_pause_args), + COMMON_OPTIONS(g_lcr_cmd_pause_args) + }; + + lcr_arguments_init(&g_lcr_cmd_pause_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), + argc, + (const char **)argv, + g_lcr_cmd_pause_desc, g_lcr_cmd_pause_usage); + if (command_parse_args(&cmd, &g_lcr_cmd_pause_args.argc, &g_lcr_cmd_pause_args.argv)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_pause_args.name, g_lcr_cmd_pause_args.log_file, + g_lcr_cmd_pause_args.log_priority, + g_lcr_cmd_pause_args.progname, + g_lcr_cmd_pause_args.quiet, + LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (g_lcr_cmd_pause_args.name == NULL) { + fprintf(stderr, "missing container name, use -n,--name option\n"); + exit(EXIT_FAILURE); + } + + if (!lcr_pause(g_lcr_cmd_pause_args.name, g_lcr_cmd_pause_args.lcrpath)) { + fprintf(stderr, "Failed to pause container %s\n", g_lcr_cmd_pause_args.name); + exit(EXIT_FAILURE); + } + + fprintf(stdout, "Container \"%s\" paused\n", g_lcr_cmd_pause_args.name); + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/pause.h b/src/cmd/pause.h new file mode 100644 index 0000000..ae26d62 --- /dev/null +++ b/src/cmd/pause.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * 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 pause definition + ******************************************************************************/ +#ifndef __CMD_PAUSE_H +#define __CMD_PAUSE_H + +#include "arguments.h" +#include "commander.h" + +#define PAUSE_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL } + +extern const char g_lcr_cmd_pause_desc[]; +extern struct lcr_arguments g_lcr_cmd_pause_args; +int cmd_pause_main(int argc, const char **argv); + +#endif /* __CMD_PAUSE_H */ diff --git a/src/cmd/resume.c b/src/cmd/resume.c new file mode 100644 index 0000000..0d6ac72 --- /dev/null +++ b/src/cmd/resume.c @@ -0,0 +1,60 @@ +/****************************************************************************** + * 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 resume functions + ******************************************************************************/ +#include +#include + +#include "lcrcontainer.h" + +#include "arguments.h" +#include "resume.h" +#include "utils.h" +#include "log.h" + +const char g_lcr_cmd_resume_desc[] = "Resume container in specified container NAME"; +static const char g_lcr_cmd_resume_usage[] = "resume [command options] --name=NAME"; + +struct lcr_arguments g_lcr_cmd_resume_args; + +int cmd_resume_main(int argc, const char **argv) +{ + command_t cmd; + struct command_option options[] = { RESUME_OPTIONS(g_lcr_cmd_resume_args), COMMON_OPTIONS(g_lcr_cmd_resume_args) }; + + lcr_arguments_init(&g_lcr_cmd_resume_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, g_lcr_cmd_resume_desc, + g_lcr_cmd_resume_usage); + if (command_parse_args(&cmd, &g_lcr_cmd_resume_args.argc, &g_lcr_cmd_resume_args.argv)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_resume_args.name, g_lcr_cmd_resume_args.log_file, g_lcr_cmd_resume_args.log_priority, + g_lcr_cmd_resume_args.progname, g_lcr_cmd_resume_args.quiet, LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (g_lcr_cmd_resume_args.name == NULL) { + fprintf(stderr, "missing container name, use -n,--name option\n"); + exit(EXIT_FAILURE); + } + + if (!lcr_resume(g_lcr_cmd_resume_args.name, g_lcr_cmd_resume_args.lcrpath)) { + fprintf(stderr, "Failed to resume container %s\n", g_lcr_cmd_resume_args.name); + exit(EXIT_FAILURE); + } + + fprintf(stdout, "Container \"%s\" resumed\n", g_lcr_cmd_resume_args.name); + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/resume.h b/src/cmd/resume.h new file mode 100644 index 0000000..c867c22 --- /dev/null +++ b/src/cmd/resume.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * 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 resume definition + ******************************************************************************/ +#ifndef __CMD_RESUME_H +#define __CMD_RESUME_H + +#include "arguments.h" +#include "commander.h" + +#define RESUME_OPTIONS(cmdargs) \ + {CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL} + +extern const char g_lcr_cmd_resume_desc[]; +extern struct lcr_arguments g_lcr_cmd_resume_args; +int cmd_resume_main(int argc, const char **argv); + +#endif /* __CMD_RESUME_H */ diff --git a/src/cmd/spec.c b/src/cmd/spec.c new file mode 100644 index 0000000..e052520 --- /dev/null +++ b/src/cmd/spec.c @@ -0,0 +1,209 @@ +/****************************************************************************** + * 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 spec functions + ******************************************************************************/ +#include +#include +#include + +#include "lcrcontainer.h" +#include "lcrcontainer_extend.h" +#include "oci_runtime_spec.h" + +#include "arguments.h" +#include "spec.h" +#include "log.h" +#include "utils.h" + +const char g_lcr_cmd_spec_desc[] = + "Create a new specification file."; +static const char g_lcr_cmd_spec_usage[] = "spec [command options]"; + +struct lcr_arguments g_lcr_cmd_spec_args; + +static void determine_distribution(const struct lcr_arguments *spec_args, + struct lcr_list **lcr_conf, char **seccomp_conf) +{ + char *distribution = NULL; + + if (spec_args->spec_dist != NULL) { + distribution = util_strdup_s(spec_args->spec_dist); + } else { + distribution = util_strdup_s("app"); + } + *lcr_conf = lcr_dist2spec(distribution, seccomp_conf); + + free(distribution); +} + +static int check_lcr_config(const struct lcr_arguments *spec_args, const struct lcr_list *lcr_conf) +{ + if (lcr_conf == NULL) { + if (spec_args->spec_dist != NULL) { + ERROR("Create distribution specific configuration failed"); + } else { + ERROR("Translate oci specification to lcr configuration failed"); + } + return -1; + } + + return 0; +} + +static int get_lcr_conf(const struct lcr_arguments *spec_args, + struct lcr_list **lcr_conf, char **seccomp_conf) +{ + int ret = -1; + oci_runtime_spec *container = NULL; + + if (spec_args->spec_translate == NULL) { + determine_distribution(spec_args, lcr_conf, seccomp_conf); + } else { + if (!container_parse(spec_args->spec_translate, NULL, &container)) { + ERROR("Failed to parse container!"); + goto out; + } + *lcr_conf = lcr_oci2lcr(NULL, NULL, container, seccomp_conf); + } + + if (check_lcr_config(spec_args, *lcr_conf) != 0) { + goto out; + } + + ret = 0; + +out: + free_oci_runtime_spec(container); + return ret; +} + +static int get_lcr_fake_path_name(const char *bundle, char **fake_path, + char **fake_name) +{ + int ret = -1; + size_t len = 0; + size_t slash_index = 0; + + len = strlen(bundle); + for (slash_index = len - 1; slash_index > 0; + slash_index--) { + if (bundle[slash_index] == '/') { + break; + } + } + *fake_path = util_common_calloc_s(slash_index + 1); + if (*fake_path == NULL) { + goto out; + } + + if (strncpy_s(*fake_path, slash_index + 1, bundle, slash_index) != EOK) { + ERROR("Failed to copy string!"); + goto out; + } + (*fake_path)[slash_index] = '\0'; + + *fake_name = util_common_calloc_s((len - slash_index) + 1); + if (*fake_name == NULL) { + goto out; + } + + if (strncpy_s(*fake_name, (len - slash_index) + 1, &(bundle[slash_index + 1]), + len - slash_index) != EOK) { + ERROR("Failed to copy string!"); + goto out; + } + (*fake_name)[(len - slash_index) - 1] = '\0'; + + ret = 0; +out: + return ret; +} + +static int check_spec_dist_and_translate() +{ + if ((g_lcr_cmd_spec_args.spec_dist != NULL) && (g_lcr_cmd_spec_args.spec_translate != NULL)) { + ERROR("-t can't be used with --dist"); + return -1; + } + + return 0; +} + +static int get_spec_bundle(char *bundle, size_t len) +{ + if (g_lcr_cmd_spec_args.spec_bundle == NULL) { + if (getcwd(bundle, len) == NULL) { + ERROR("getcwd failed"); + return -1; + } + } else if (strlen(g_lcr_cmd_spec_args.spec_bundle) >= PATH_MAX || + realpath(g_lcr_cmd_spec_args.spec_bundle, bundle) == NULL) { + ERROR("failed to get absolute path '%s'\n", g_lcr_cmd_spec_args.spec_bundle); + return -1; + } + + return 0; +} + +int cmd_spec_main(int argc, const char **argv) +{ + int ret = -1; + char *fakepath = NULL; + char *fakename = NULL; + char *seccomp = NULL; + char bundle[PATH_MAX] = { 0 }; + struct lcr_list *lcr_conf = NULL; + command_t cmd; + struct command_option options[] = { + SPEC_OPTIONS(g_lcr_cmd_spec_args), + COMMON_OPTIONS(g_lcr_cmd_spec_args) + }; + + lcr_arguments_init(&g_lcr_cmd_spec_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, + (const char **)argv, g_lcr_cmd_spec_desc, g_lcr_cmd_spec_usage); + if (command_parse_args(&cmd, &g_lcr_cmd_spec_args.argc, &g_lcr_cmd_spec_args.argv)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(NULL, g_lcr_cmd_spec_args.log_file, g_lcr_cmd_spec_args.log_priority, + g_lcr_cmd_spec_args.progname, g_lcr_cmd_spec_args.quiet, LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (check_spec_dist_and_translate() != 0) { + exit(EXIT_FAILURE); + } + + if (get_spec_bundle(bundle, PATH_MAX) != 0) { + exit(EXIT_FAILURE); + } + + if (get_lcr_fake_path_name(bundle, &fakepath, &fakename) || + get_lcr_conf(&g_lcr_cmd_spec_args, &lcr_conf, &seccomp)) { + goto out; + } + + if (!lcr_save_spec(fakename, fakepath, lcr_conf, seccomp)) { + goto out; + } + ret = 0; +out: + lcr_free_config(lcr_conf); + free(lcr_conf); + free(seccomp); + free(fakepath); + free(fakename); + return ret; +} diff --git a/src/cmd/spec.h b/src/cmd/spec.h new file mode 100644 index 0000000..04fbd5f --- /dev/null +++ b/src/cmd/spec.h @@ -0,0 +1,33 @@ +/****************************************************************************** + * 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 spec definition + ******************************************************************************/ +#ifndef __CMD_SPEC_H +#define __CMD_SPEC_H + +#include "arguments.h" +#include "commander.h" + +#define SPEC_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "bundle", 'b', &(cmdargs).spec_bundle, \ + "Path to the root of the bundle directory, default is current directory", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "translate", 't', &(cmdargs).spec_translate, \ + "Translate oci specification (in json format) to lcr configuration", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "dist", 0, &(cmdargs).spec_dist, \ + "Generate distribution specification, now support: ubuntu", NULL } + +extern const char g_lcr_cmd_spec_desc[]; +extern struct lcr_arguments g_lcr_cmd_spec_args; +int cmd_spec_main(int argc, const char **argv); + +#endif /* __CMD_SPEC_H */ diff --git a/src/cmd/start.c b/src/cmd/start.c new file mode 100644 index 0000000..b811f3f --- /dev/null +++ b/src/cmd/start.c @@ -0,0 +1,111 @@ +/****************************************************************************** + * 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 start functions + ******************************************************************************/ +#include +#include +#include + +#include "lcrcontainer.h" +#include "start.h" +#include "log.h" +#include "utils.h" + +const char g_lcr_cmd_start_desc[] = "start container"; +static const char g_lcr_cmd_start_usage[] = "start [command options] --name=NAME"; + +struct lcr_arguments g_lcr_cmd_start_args; +int callback_foreground(command_option_t *option, const char *arg) +{ + struct lcr_arguments *args = (struct lcr_arguments *)option->data; + if (arg == NULL) { + return 0; + } + if (!strcmp(arg, "true")) { + args->start_daemonize = false; + } else if (!strcmp(arg, "false")) { + args->start_daemonize = true; + } else { + return -1; + } + return 0; +} +int cmd_start_main(int argc, const char **argv) +{ + command_t cmd; + struct command_option options[] = { + START_OPTIONS(g_lcr_cmd_start_args), + COMMON_OPTIONS(g_lcr_cmd_start_args) + }; + struct lcr_start_request request = {0}; + + lcr_arguments_init(&g_lcr_cmd_start_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, + g_lcr_cmd_start_desc, g_lcr_cmd_start_usage); + + if (command_parse_args(&cmd, &g_lcr_cmd_start_args.argc, &g_lcr_cmd_start_args.argv) || + start_checker(&g_lcr_cmd_start_args)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_start_args.name, g_lcr_cmd_start_args.log_file, + g_lcr_cmd_start_args.log_priority, + g_lcr_cmd_start_args.name, + g_lcr_cmd_start_args.quiet, + LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (g_lcr_cmd_start_args.name == NULL) { + fprintf(stderr, "missing container name, use -n,--name option\n"); + exit(EXIT_FAILURE); + } + + request.name = g_lcr_cmd_start_args.name; + request.lcrpath = g_lcr_cmd_start_args.lcrpath; + request.logpath = g_lcr_cmd_start_args.log_file; + request.loglevel = g_lcr_cmd_start_args.log_priority; + request.daemonize = g_lcr_cmd_start_args.start_daemonize; + request.tty = true; + request.open_stdin = true; + request.pidfile = g_lcr_cmd_start_args.start_pidfile; + request.console_fifos = g_lcr_cmd_start_args.console_fifos; + request.console_logpath = g_lcr_cmd_start_args.console_logpath; + + if (!lcr_start(&request)) { + ERROR("Failed to start container %s", g_lcr_cmd_start_args.name); + exit(EXIT_FAILURE); + } + + if (g_lcr_cmd_start_args.start_daemonize) { + fprintf(stdout, "Container \"%s\" started\n", g_lcr_cmd_start_args.name); + } + exit(EXIT_SUCCESS); +} + +static bool check_start_arguments(const struct lcr_arguments *args) +{ + return (args->console_fifos[0] && !args->console_fifos[1]) || + (!args->console_fifos[0] && args->console_fifos[1]); +} + +int start_checker(const struct lcr_arguments *args) +{ + if (check_start_arguments(args)) { + fprintf(stderr, "Should specify the input and output FIFOs at the same time\n"); + return -1; + } + + return 0; +} diff --git a/src/cmd/start.h b/src/cmd/start.h new file mode 100644 index 0000000..0bb69b9 --- /dev/null +++ b/src/cmd/start.h @@ -0,0 +1,39 @@ +/****************************************************************************** + * 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 start definition + ******************************************************************************/ +#ifndef __CMD_START_H +#define __CMD_START_H + +#include "arguments.h" +#include "commander.h" + +#define START_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL }, \ + { CMD_OPT_TYPE_BOOL, false, "daemon", 'd', &(cmdargs).start_daemonize, \ + "Daemonize the container (default)", NULL }, \ + { CMD_OPT_TYPE_CALLBACK, false, "foreground", 'F', &(cmdargs).start_daemonize, \ + "Start with the current tty attached to /dev/console", callback_foreground }, \ + { CMD_OPT_TYPE_STRING, false, "pidfile", 0, &(cmdargs).start_pidfile, "Create a file with the process id", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "console_file", 'L', &(cmdargs).console_logpath, \ + "Save the console output to the file", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "in-fifo", 0, &(cmdargs).console_fifos[0], "console fifo", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "out-fifo", 0, &(cmdargs).console_fifos[1], "console fifo", NULL } + +extern const char g_lcr_cmd_start_desc[]; +extern struct lcr_arguments g_lcr_cmd_start_args; +int callback_foreground(command_option_t *option, const char *arg); +int start_checker(const struct lcr_arguments *args); +int cmd_start_main(int argc, const char **argv); + +#endif /* __CMD_START_H */ diff --git a/src/cmd/state.c b/src/cmd/state.c new file mode 100644 index 0000000..46b9e18 --- /dev/null +++ b/src/cmd/state.c @@ -0,0 +1,194 @@ +/****************************************************************************** + * 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 state functions + ******************************************************************************/ +#include +#include +#include +#include + +#include "lcrcontainer.h" +#include "state.h" +#include "arguments.h" +#include "log.h" +#include "utils.h" + +const char g_lcr_cmd_state_desc[] = "Output the state of a container"; +static const char g_lcr_cmd_state_usage[] = "state --name=NAME"; + +struct lcr_arguments g_lcr_cmd_state_args; + +static uint64_t read_memory_info(void) +{ + uint64_t sysmem_limit = 0; + size_t len = 0; + char *line = NULL; + char *p = NULL; + + FILE *fp = util_fopen("/proc/meminfo", "r"); + if (fp == NULL) { + ERROR("Failed to open /proc/meminfo"); + return sysmem_limit; + } + + while (getline(&line, &len, fp) != -1) { + p = strchr(line, ' '); + if (p == NULL) { + goto out; + } + *p = '\0'; + p++; + if (strcmp(line, "MemTotal:") == 0) { + while (*p != '\0' && (*p == ' ' || *p == '\t')) { + p++; + } + if (*p == '\0') { + goto out; + } + sysmem_limit = strtoull(p, NULL, 0); + break; + } + } + +out: + fclose(fp); + free(line); + return sysmem_limit * SIZE_KB; +} + +static void size_humanize(unsigned long long val, char *buf, size_t bufsz) +{ + errno_t ret; + + if (val > 1 << 30) { + ret = sprintf_s(buf, bufsz, "%u.%2.2u GiB", + (unsigned int)(val >> 30), + (unsigned int)((val & ((1 << 30) - 1)) * 100) >> 30); + } else if (val > 1 << 20) { + unsigned long long x = val + 5243; /* for rounding */ + ret = sprintf_s(buf, bufsz, "%u.%2.2u MiB", + (unsigned int)(x >> 20), (unsigned int)(((x & ((1 << 20) - 1)) * 100) >> 20)); + } else if (val > 1 << 10) { + unsigned long long x = val + 5; /* for rounding */ + ret = sprintf_s(buf, bufsz, "%u.%2.2u KiB", + (unsigned int)(x >> 10), (unsigned int)(((x & ((1 << 10) - 1)) * 100) >> 10)); + } else { + ret = sprintf_s(buf, bufsz, "%u bytes", (unsigned int)val); + } + + if (ret < 0) { + ERROR("Failed to sprintf string"); + return; + } +} + +static void print_state(const struct lcr_container_state *lcs) +{ + char buf[BUFSIZE]; + + fprintf(stdout, "%-15s %s\n", "Name:", lcs->name); + fprintf(stdout, "%-15s %s\n", "State:", lcs->state); + if (strcmp(lcs->state, "RUNNING") != 0) { + return; + } + + fprintf(stdout, "%-15s %d\n", "PID:", lcs->init); + fprintf(stdout, "%-15s %.2f seconds\n", "CPU use:", + (double)lcs->cpu_use_nanos / 1000000000.0); + + fprintf(stdout, "%-15s %llu\n", "Pids current:", + (unsigned long long)lcs->pids_current); + + size_humanize(lcs->mem_used, buf, sizeof(buf)); + fprintf(stdout, "%-15s %s\n", "Memory use:", buf); + + size_humanize(lcs->mem_limit, buf, sizeof(buf)); + fprintf(stdout, "%-15s %s\n", "Memory limit:", buf); + + size_humanize(lcs->kmem_used, buf, sizeof(buf)); + fprintf(stdout, "%-15s %s\n", "KMem use:", buf); + + size_humanize(lcs->kmem_limit, buf, sizeof(buf)); + fprintf(stdout, "%-15s %s\n", "KMem limit:", buf); + + size_humanize(lcs->io_service_bytes.read, buf, sizeof(buf)); + fprintf(stdout, "%-15s %s\n", "Blkio read:", buf); + + size_humanize(lcs->io_service_bytes.write, buf, sizeof(buf)); + fprintf(stdout, "%-15s %s\n", "Blkio write:", buf); +} + +static inline int check_container_name() +{ + if (g_lcr_cmd_state_args.name == NULL) { + fprintf(stderr, "missing --name,-n option\n"); + return -1; + } + + return 0; +} + +static void set_sysmem_limit(struct lcr_container_state *state) +{ + uint64_t sysmem_limit = 0; + + sysmem_limit = read_memory_info(); + if (sysmem_limit > 0 && state->mem_limit > sysmem_limit) { + state->mem_limit = sysmem_limit; + } + if (sysmem_limit > 0 && state->kmem_limit > sysmem_limit) { + state->kmem_limit = sysmem_limit; + } +} + +int cmd_state_main(int argc, const char **argv) +{ + struct lcr_container_state state = { 0 }; + command_t cmd; + struct command_option options[] = { + STATE_OPTIONS(g_lcr_cmd_state_args), + COMMON_OPTIONS(g_lcr_cmd_state_args) + }; + + lcr_arguments_init(&g_lcr_cmd_state_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, + g_lcr_cmd_state_desc, g_lcr_cmd_state_usage); + if (command_parse_args(&cmd, &g_lcr_cmd_state_args.argc, &g_lcr_cmd_state_args.argv)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_state_args.name, + g_lcr_cmd_state_args.log_file, + g_lcr_cmd_state_args.log_priority, + g_lcr_cmd_state_args.progname, + g_lcr_cmd_state_args.quiet, + LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (check_container_name() != 0) { + exit(EXIT_FAILURE); + } + + if (!lcr_state(g_lcr_cmd_state_args.name, g_lcr_cmd_state_args.lcrpath, &state)) { + fprintf(stderr, "Error get the container \"%s\"'s state\n", g_lcr_cmd_state_args.name); + exit(EXIT_FAILURE); + } + + set_sysmem_limit(&state); + + print_state(&state); + lcr_container_state_free(&state); + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/state.h b/src/cmd/state.h new file mode 100644 index 0000000..f8bcc86 --- /dev/null +++ b/src/cmd/state.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * 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 state definition + ******************************************************************************/ +#ifndef __CMD_STATE_H +#define __CMD_STATE_H + +#include "arguments.h" +#include "commander.h" + +#define STATE_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL } + +extern const char g_lcr_cmd_state_desc[]; +extern struct lcr_arguments g_lcr_cmd_state_args; +int cmd_state_main(int argc, const char **argv); + +#endif /* __CMD_STATE_H */ diff --git a/src/cmd/update.c b/src/cmd/update.c new file mode 100644 index 0000000..e4033f1 --- /dev/null +++ b/src/cmd/update.c @@ -0,0 +1,122 @@ +/****************************************************************************** + * 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 update functions + ******************************************************************************/ +#include +#include +#include +#include + +#include "lcrcontainer.h" +#include "update.h" +#include "arguments.h" +#include "log.h" +#include "utils.h" + +#define BLKIOWEIGHT 1 +#define CPUSHARES 2 +#define CPUPERIOD 3 +#define CPUQUOTA 4 +#define CPUSETCPUS 5 +#define CPUSETMEMS 6 +#define KERNELMEMORY 7 +#define MEMORYRESERV 8 +#define MEMORYSWAP 9 + +const char g_lcr_cmd_update_desc[] = "Update configuration of a container"; +static const char g_lcr_cmd_update_usage[] = "update --name=NAME"; + +struct lcr_arguments g_lcr_cmd_update_args; + +static void to_cgroup_cpu_resources(const struct lcr_arguments *args, struct lcr_cgroup_resources *cr) +{ + if (args->cr.blkio_weight) { + cr->blkio_weight = strtoull(args->cr.blkio_weight, NULL, 10); + } + if (args->cr.cpu_shares) { + cr->cpu_shares = strtoull(args->cr.cpu_shares, NULL, 10); + } + if (args->cr.cpu_period) { + cr->cpu_period = strtoull(args->cr.cpu_period, NULL, 10); + } + if (args->cr.cpu_quota) { + cr->cpu_quota = strtoull(args->cr.cpu_quota, NULL, 10); + } + if (args->cr.cpuset_cpus) { + cr->cpuset_cpus = args->cr.cpuset_cpus; + } + if (args->cr.cpuset_mems) { + cr->cpuset_mems = args->cr.cpuset_mems; + } +} + +static void to_cgroup_mem_resources(const struct lcr_arguments *args, struct lcr_cgroup_resources *cr) +{ + if (args->cr.kernel_memory_limit) { + cr->kernel_memory_limit = strtoull(args->cr.kernel_memory_limit, NULL, 10); + } + if (args->cr.memory_reservation) { + cr->memory_reservation = strtoull(args->cr.memory_reservation, NULL, 10); + } + if (args->cr.memory_limit) { + cr->memory_limit = strtoull(args->cr.memory_limit, NULL, 10); + } + if (args->cr.memory_swap) { + cr->memory_swap = strtoull(args->cr.memory_swap, NULL, 10); + } +} + +static void to_cgroup_resources(const struct lcr_arguments *args, struct lcr_cgroup_resources *cr) +{ + if (args == NULL || cr == NULL) { + return; + } + + to_cgroup_cpu_resources(args, cr); + + to_cgroup_mem_resources(args, cr); +} + +int cmd_update_main(int argc, const char **argv) +{ + command_t cmd; + struct lcr_cgroup_resources cr = { 0 }; + struct command_option options[] = { UPDATE_OPTIONS(g_lcr_cmd_update_args), COMMON_OPTIONS(g_lcr_cmd_update_args) }; + + lcr_arguments_init(&g_lcr_cmd_update_args); + + command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, g_lcr_cmd_update_desc, + g_lcr_cmd_update_usage); + + if (command_parse_args(&cmd, &g_lcr_cmd_update_args.argc, &g_lcr_cmd_update_args.argv)) { + exit(EINVALIDARGS); + } + + if (lcr_log_init(g_lcr_cmd_update_args.name, g_lcr_cmd_update_args.log_file, g_lcr_cmd_update_args.log_priority, + g_lcr_cmd_update_args.progname, g_lcr_cmd_update_args.quiet, LOGPATH)) { + exit(EXIT_FAILURE); + } + + if (g_lcr_cmd_update_args.name == NULL) { + fprintf(stderr, "missing --name,-n option\n"); + exit(EXIT_FAILURE); + } + + to_cgroup_resources(&g_lcr_cmd_update_args, &cr); + if (!lcr_update(g_lcr_cmd_update_args.name, g_lcr_cmd_update_args.lcrpath, &cr)) { + fprintf(stderr, "Error update container %s\n", g_lcr_cmd_update_args.name); + exit(EXIT_FAILURE); + } + + exit(EXIT_SUCCESS); +} diff --git a/src/cmd/update.h b/src/cmd/update.h new file mode 100644 index 0000000..9733a2d --- /dev/null +++ b/src/cmd/update.h @@ -0,0 +1,46 @@ +/****************************************************************************** + * 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 update definition + ******************************************************************************/ +#ifndef __CMD_UPDATE_H +#define __CMD_UPDATE_H + +#include "arguments.h" +#include "commander.h" + +#define UPDATE_OPTIONS(cmdargs) \ + { CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "blkio-weight", 0, &(cmdargs).cr.blkio_weight, \ + "Block IO (relative weight), between 10 and 1000", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "cpu-shares", 0, &(cmdargs).cr.cpu_shares, "CPU shares (relative weight)", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "cpu-period", 0, &(cmdargs).cr.cpu_period, \ + "Limit CPU CFS (Completely Fair Scheduler) period", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "cpu-quota", 0, &(cmdargs).cr.cpu_quota, \ + "Limit CPU CFS (Completely Fair Scheduler) quota", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "cpuset-cpus", 0, &(cmdargs).cr.cpuset_cpus, \ + "CPUs in which to allow execution (0-3, 0,1)", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "cpuset-mems", 0, &(cmdargs).cr.cpuset_mems, \ + "MEMs in which to allow execution (0-3, 0,1)", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "kernel-memory", 0, &(cmdargs).cr.kernel_memory_limit, \ + "Kernel memory limit", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "memory", 'm', &(cmdargs).cr.memory_limit, "Memory limit", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "memory-reservation", 0, &(cmdargs).cr.memory_reservation, \ + "Memory soft limit", NULL }, \ + { CMD_OPT_TYPE_STRING, false, "memory-swap", 0, &(cmdargs).cr.memory_swap, \ + "Swap limit equal to memory plus swap: '-1' to enable unlimited swap", NULL } + +extern const char g_lcr_cmd_update_desc[]; +extern struct lcr_arguments g_lcr_cmd_update_args; +int cmd_update_main(int argc, const char **argv); + +#endif /* __CMD_UPDATE_H */ diff --git a/src/conf.c b/src/conf.c new file mode 100644 index 0000000..1f4319f --- /dev/null +++ b/src/conf.c @@ -0,0 +1,2611 @@ +/****************************************************************************** + * 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 conf function + ******************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "conf.h" +#include "lcrcontainer.h" +#include "lcrcontainer_extend.h" +#include "error.h" +#include "utils.h" +#include "log.h" +#include "buffer.h" +#include "securec.h" + +#define SUB_UID_PATH "/etc/subuid" +#define SUB_GID_PATH "/etc/subgid" +#define ID_MAP_LEN 100 + +/* files limit checker */ +static int files_limit_checker(const char *value) +{ + long long limit = 0; + int ret = 0; + + ret = util_safe_llong(value, &limit); + if (ret) { + ret = -1; + } + + return ret; +} + +/* check console log file */ +static int check_console_log_file(const char *value) +{ + int ret = 0; + + if (value == NULL) { + return -1; + } + + if (strcmp(value, "none") == 0) { + ret = 1; + } + + return ret; +} + +/* check console log filesize */ +static int check_console_log_filesize(const char *value) +{ + int ret = -1; + int64_t tmp = 0; + int64_t min = 4 * SIZE_KB; + + if (value == NULL) { + return ret; + } + + if (parse_byte_size_string(value, &tmp) == 0 && tmp >= min) { + ret = 0; + } + + return ret; +} + +/* check oom score adj */ +static int check_oom_score_adj(const char *value) +{ + int ret = -1; + int tmp = 0; + int min = OOM_SCORE_ADJ_MIN; + int max = OOM_SCORE_ADJ_MAX; + + if (value == NULL) { + return ret; + } + + if (util_safe_int(value, &tmp) == 0 && tmp >= min && tmp <= max) { + ret = 0; + } + lcr_set_error_message(LCR_ERR_RUNTIME, "Invalid value %s, range for oom score adj is [%d, %d]", value, min, max); + return ret; +} +/* check console log filerotate */ +static int check_console_log_filerotate(const char *value) +{ + int ret = -1; + unsigned int tmp = 0; + + if (value == NULL) { + return ret; + } + + if (util_safe_uint(value, &tmp) == 0) { + ret = 0; + } + + return ret; +} + +/* check rootfs mount */ +static int check_rootfs_mount(const char *value) +{ + if (value == NULL) { + return -1; + } + + if (!dir_exists(value)) { + lcr_set_error_message(LCR_ERR_RUNTIME, "Container rootfs mount path '%s' is not exist", value); + return -1; + } + + return 0; +} + +static inline bool is_native_umask_normal(const char *value) +{ + return strcmp(value, "normal") == 0; +} + +static inline bool is_native_umask_secure(const char *value) +{ + return strcmp(value, "secure") == 0; +} + + +/* check umask */ +static int check_native_umask(const char *value) +{ + if (value == NULL) { + return -1; + } + + if (!is_native_umask_normal(value) && !is_native_umask_secure(value)) { + ERROR("Invalid native umask: %s", value); + return -1; + } + + return 0; +} + +/* check system container */ +static int check_system_container(const char *value) +{ + if (value == NULL) { + return -1; + } + + if (strcmp(value, "true") != 0) { + ERROR("Invalid system container: %s", value); + return -1; + } + + return 0; +} + +/* check cgroup dir */ +static int check_cgroup_dir(const char *value) +{ + if (value == NULL) { + return -1; + } + return 0; +} + +static const lcr_annotation_item_t g_require_annotations[] = { + { + "files.limit", + "lxc.cgroup.files.limit", + files_limit_checker, + }, + { + "log.console.file", + "lxc.console.logfile", + check_console_log_file, + }, + { + "log.console.filesize", + "lxc.console.size", + check_console_log_filesize, + }, + { + "log.console.filerotate", + "lxc.console.rotate", + check_console_log_filerotate, + }, + { + "rootfs.mount", + "lxc.rootfs.mount", + check_rootfs_mount, + }, + { + "cgroup.dir", + "lxc.cgroup.dir", + check_cgroup_dir, + }, + { + "native.umask", + "lxc.isulad.umask", + check_native_umask, + }, + { + "system.container", + "lxc.isulad.systemd", + check_system_container, + }, + { + "proc.oom_score_adj", + "lxc.proc.oom_score_adj", + check_oom_score_adj, + }, +}; + +/* create lcr list node */ +struct lcr_list *create_lcr_list_node(const char *key, const char *value) +{ + struct lcr_list *node = NULL; + lcr_config_item_t *entry = NULL; + + node = util_common_calloc_s(sizeof(*node)); + if (node == NULL) { + return NULL; + } + entry = util_common_calloc_s(sizeof(*entry)); + if (entry == NULL) { + free(node); + return NULL; + } + entry->name = util_strdup_s(key); + + entry->value = util_strdup_s(value); + + node->elem = entry; + return node; +} + +/* free lcr list node */ +void free_lcr_list_node(struct lcr_list *node) +{ + lcr_config_item_t *entry = NULL; + + if (node == NULL) { + return; + } + + entry = node->elem; + if (entry != NULL) { + free(entry->name); + free(entry->value); + } + free(node->elem); + node->elem = NULL; + free(node); +} + +/* trans oci hostname */ +struct lcr_list *trans_oci_hostname(const char *hostname) +{ + if (hostname == NULL) { + return NULL; + } + + return create_lcr_list_node("lxc.uts.name", hostname); +} + +static bool valid_sep_len(size_t sep_len, size_t len) +{ + if (sep_len && (sep_len != 1) && (len > SIZE_MAX / sep_len + 1)) { + return false; + } + + return true; +} + +/* capabilities join */ +static char *capabilities_join(const char *sep, const char **parts, size_t len) +{ + char *result = NULL; + size_t sep_len; + size_t result_len; + size_t iter; + int nret = 0; + + sep_len = strlen(sep); + if (valid_sep_len(sep_len, len) == false) { + return NULL; + } + result_len = (len - 1) * sep_len; + /* calculate new string length + * dont calculate `CAP_` + */ + for (iter = 0; iter < len; iter++) { + if (result_len > 4 && (result_len - 4 >= SIZE_MAX - strlen(parts[iter]))) { + return NULL; + } + result_len += strlen(parts[iter]) - 4; + } + + result = calloc(result_len + 1, 1); + if (result == NULL) { + return NULL; + } + + for (iter = 0; iter < len - 1; iter++) { + nret = strcat_s(result, result_len + 1, &(parts[iter][4])); + if (nret != EOK) { + goto err_out; + } + + nret = strcat_s(result, result_len + 1, sep); + if (nret != EOK) { + goto err_out; + } + } + nret = strcat_s(result, result_len + 1, &(parts[len - 1][4])); + if (nret != EOK) { + goto err_out; + } + + // Lower case + for (iter = 0; iter < result_len; iter++) { + if (result[iter] >= 'A' && result[iter] <= 'Z') { + result[iter] = (char)(result[iter] + 32); + } + } + + return result; + +err_out: + free(result); + return NULL; +} + +#define UID_MAX_SIZE 21 +/* UID to use within a private user namespace for init */ +static int trans_oci_process_init_uid(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + char buf[UID_MAX_SIZE] = { 0 }; + int nret; + int ret = -1; + if (proc->user != NULL && proc->user->uid != INVALID_INT) { + nret = sprintf_s(buf, sizeof(buf), "%u", (unsigned int)proc->user->uid); + if (nret < 0) { + goto out; + } + + node = create_lcr_list_node("lxc.init.uid", buf); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +/* GID to use within a private user namespace for init */ +static int trans_oci_process_init_gid(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + char buf[UID_MAX_SIZE] = { 0 }; + int nret; + int ret = -1; + if (proc->user != NULL && proc->user->gid != INVALID_INT) { + nret = sprintf_s(buf, sizeof(buf), "%u", (unsigned int)proc->user->gid); + if (nret < 0) { + goto out; + } + + node = create_lcr_list_node("lxc.init.gid", buf); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +/* additional groups for init command */ +static int trans_oci_process_init_groups(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int nret; + size_t i = 0; + int ret = -1; + if (proc->user != NULL && proc->user->additional_gids != NULL && proc->user->additional_gids_len > 0) { + if (proc->user->additional_gids_len > (SIZE_MAX / (LCR_NUMSTRLEN64 + 1))) { + goto out; + } + + size_t total_len = (LCR_NUMSTRLEN64 + 1) * proc->user->additional_gids_len; + char *gids = util_common_calloc_s(total_len); + if (gids == NULL) { + goto out; + } + + nret = sprintf_s(gids, total_len, "%u", (unsigned int)(proc->user->additional_gids[0])); + if (nret < 0) { + free(gids); + goto out; + } + for (i = 1; i < proc->user->additional_gids_len; i++) { + size_t old_len = strlen(gids); + nret = sprintf_s(gids + old_len, total_len - old_len, " %u", + (unsigned int)(proc->user->additional_gids[i])); + if (nret < 0) { + free(gids); + goto out; + } + } + + node = create_lcr_list_node("lxc.isulad.init.groups", gids); + free(gids); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +/* Sets the command to use as the init system for the containers */ +static int trans_oci_process_init_args(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + size_t i = 0; + int ret = -1; + for (i = 0; i < proc->args_len; i++) { + node = create_lcr_list_node("lxc.isulad.init.args", proc->args[i]); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +/* working directory to use within container */ +static int trans_oci_process_init_cwd(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + if (proc->cwd != NULL) { + node = create_lcr_list_node("lxc.init.cwd", proc->cwd); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +/* trans oci process init */ +static int trans_oci_process_init(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + int ret = -1; + if (trans_oci_process_init_uid(proc, conf)) { + goto out; + } + + if (trans_oci_process_init_gid(proc, conf)) { + goto out; + } + + if (trans_oci_process_init_groups(proc, conf)) { + goto out; + } + + if (trans_oci_process_init_args(proc, conf)) { + goto out; + } + + if (trans_oci_process_init_cwd(proc, conf)) { + goto out; + } + + ret = 0; +out: + return ret; +} + +/* trans oci process env and cap */ +static int trans_oci_process_env_and_cap(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + char *boundings = NULL; + int ret = -1; + size_t i; + + for (i = 0; i < proc->env_len; i++) { + char *replaced = util_string_replace(" ", SPACE_MAGIC_STR, proc->env[i]); + if (replaced == NULL) { + ERROR("memory allocation error"); + goto out; + } + node = create_lcr_list_node("lxc.environment", replaced); + free(replaced); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + + if (proc->capabilities != NULL && proc->capabilities->bounding_len > 0) { + boundings = + capabilities_join(" ", (const char **)(proc->capabilities->bounding), proc->capabilities->bounding_len); + if (boundings == NULL) { + ERROR("Failed to join bounding capabilities"); + goto out; + } + node = create_lcr_list_node("lxc.cap.keep", boundings); + free(boundings); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } else { + node = create_lcr_list_node("lxc.cap.keep", "ISULAD_KEEP_NONE"); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +/* trans oci process prlimit */ +static int trans_oci_process_prlimit(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + int nret; + size_t i; + + for (i = 0; i < proc->rlimits_len; i++) { + oci_runtime_spec_process_rlimits_element *lr = proc->rlimits[i]; + char buf_key[30] = { 0 }; + char buf_value[60] = { 0 }; + size_t j; + char *type = util_strdup_s(lr->type); + + // Lower case type,eg. RLIMIT_NOFILE -> RLIMIT_nofile + for (j = strlen("RLIMIT_"); j < strlen(type); j++) { + type[j] = (char)tolower(type[j]); + } + + // Skip `RLIMIT_` + nret = sprintf_s(buf_key, sizeof(buf_key), "lxc.prlimit.%s", &(type[7])); + free(type); + if (nret < 0) { + goto out; + } + + // We always use format `soft_limit:hard_limit` + nret = sprintf_s(buf_value, sizeof(buf_value), "%llu:%llu", (unsigned long long)lr->soft, + (unsigned long long)lr->hard); + if (nret < 0) { + goto out; + } + + node = create_lcr_list_node(buf_key, buf_value); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +/* trans oci process no new privs */ +static int trans_oci_process_no_new_privs(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + + if (proc->no_new_privileges) { + node = create_lcr_list_node("lxc.no_new_privs", "1"); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +static int trans_oci_process_apparmor(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + + if (proc->apparmor_profile != NULL) { + node = create_lcr_list_node("lxc.aa_profile", proc->apparmor_profile); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + + ret = 0; +out: + return ret; +} + +static int trans_oci_process_selinux(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + + if (proc->selinux_label != NULL) { + node = create_lcr_list_node("lxc.se_context", proc->selinux_label); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + + ret = 0; +out: + return ret; +} + +/* trans oci process apparmor and selinux */ +static int trans_oci_process_apparmor_and_selinux(const oci_runtime_spec_process *proc, struct lcr_list *conf) +{ + int ret = -1; + + if (trans_oci_process_apparmor(proc, conf) != 0) { + goto out; + } + + if (trans_oci_process_selinux(proc, conf) != 0) { + goto out; + } + + ret = 0; +out: + return ret; +} + +/* trans oci process */ +struct lcr_list *trans_oci_process(const oci_runtime_spec_process *proc) +{ + struct lcr_list *conf = NULL; + + conf = util_common_calloc_s(sizeof(struct lcr_list)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + if (trans_oci_process_init(proc, conf)) { + goto out_free; + } + + if (trans_oci_process_env_and_cap(proc, conf)) { + goto out_free; + } + + if (trans_oci_process_prlimit(proc, conf)) { + goto out_free; + } + + if (trans_oci_process_no_new_privs(proc, conf)) { + goto out_free; + } + + if (trans_oci_process_apparmor_and_selinux(proc, conf)) { + goto out_free; + } + + return conf; + +out_free: + lcr_free_config(conf); + free(conf); + return NULL; +} + +#define APPEND_COMMA_END_SIZE 2 +/* trans oci root rootfs */ +static int trans_oci_root_rootfs(const oci_runtime_spec_root *root, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + + if ((root != NULL) && root->path != NULL) { + if (strcmp(root->path, "/") != 0) { + node = create_lcr_list_node("lxc.rootfs.path", root->path); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + } + ret = 0; +out: + return ret; +} + +static inline bool is_root_readonly(const oci_runtime_spec_root *root) +{ + return root != NULL && root->readonly; +} + +/* trans oci root rootfsoptions */ +static int trans_oci_root_rootfs_options(const oci_runtime_spec_root *root, struct lcr_list *conf, + const oci_runtime_config_linux *linux) +{ + struct lcr_list *node = NULL; + char *value = NULL; + char *tmpvalue = NULL; + int ret = -1; + int nret; + + if (is_root_readonly(root)) { + value = util_strdup_s("ro"); + } + + if ((linux != NULL) && linux->rootfs_propagation != NULL) { + if (value != NULL) { + size_t newsize; + if (strlen(value) > (SIZE_MAX - strlen(linux->rootfs_propagation)) - APPEND_COMMA_END_SIZE) { + ERROR("Out of range!"); + goto out; + } + newsize = strlen(linux->rootfs_propagation) + strlen(value) + APPEND_COMMA_END_SIZE; + nret = mem_realloc((void **)&tmpvalue, newsize, value, strlen(value)); + if (nret < 0) { + ERROR("Out of memory"); + goto out; + } + value = tmpvalue; + nret = sprintf_s(value + strlen(value), newsize - strlen(value), ",%s", linux->rootfs_propagation); + if (nret < 0) { + ERROR("Failed to print string"); + goto out; + } + } else { + value = util_strdup_s(linux->rootfs_propagation); + } + } + + if (value != NULL) { + node = create_lcr_list_node("lxc.rootfs.options", value); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + free(value); + return ret; +} + +/* trans oci root */ +struct lcr_list *trans_oci_root(const oci_runtime_spec_root *root, const oci_runtime_config_linux *linux) +{ + struct lcr_list *conf = NULL; + + conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + if (trans_oci_root_rootfs(root, conf)) { + goto out_free; + } + + if (trans_oci_root_rootfs_options(root, conf, linux)) { + goto out_free; + } + + return conf; +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +static inline bool is_mount_options_invalid(const defs_mount *mount) +{ + return mount == NULL || mount->type == NULL; +} + +static inline bool is_mount_type_bind(const char *type) +{ + return strcmp(type, "bind") == 0; +} + +static inline bool is_mount_type_cgroup(const char *type) +{ + return strcmp(type, "cgroup") == 0; +} + +static inline bool is_mount_type_sysfs(const char *type) +{ + return strcmp(type, "sysfs") == 0; +} + +static inline bool is_mount_type_proc(const char *type) +{ + return strcmp(type, "proc") == 0; +} + +/* trans mount to lxc options */ +static char *trans_mount_to_lxc_options(const defs_mount *mount) +{ + char *result = NULL; + char *prefix = NULL; + char *lxc_options = NULL; + int rc; + bool isdir = true; + struct stat st; + + if (is_mount_options_invalid(mount)) { + return NULL; + } + + if (is_mount_type_bind(mount->type)) { + rc = stat(mount->source, &st); + if (rc != 0) { + ERROR("Failed to get stat of %s", mount->source); + goto free_out; + } + isdir = S_ISDIR(st.st_mode); + } + + lxc_options = isdir ? ",create=dir" : ",create=file"; + + prefix = util_string_join(",", (const char **)mount->options, mount->options_len); + if (prefix == NULL) { + prefix = util_strdup_s("defaults"); + } + + result = util_string_append(lxc_options, prefix); + free(prefix); + return result; + +free_out: + free(prefix); + free(result); + return NULL; +} + +static bool get_mount_option_ro(const defs_mount *mount) +{ + bool ro = false; + + if ((mount->options != NULL) && mount->options_len) { + size_t i = 0; + for (i = 0; i < mount->options_len; i++) { + if ((mount->options[i] != NULL) && !strcmp(mount->options[i], "ro")) { + ro = true; + break; + } + } + } + + return ro; +} + +static char *get_mount_readmode_options(const defs_mount *mount, const char *type) +{ + char *options = NULL; + bool readonly = get_mount_option_ro(mount); + + if (is_mount_type_cgroup(type)) { + if (readonly) { + options = util_strdup_s("ro:force"); + } else { + options = util_strdup_s("rw:force"); + } + } else { + if (readonly) { + options = util_strdup_s("ro"); + } else { + options = util_strdup_s("rw"); + } + } + + return options; +} + +/* trans mount auto to lxc */ +static struct lcr_list *trans_mount_auto_to_lxc(const defs_mount *mount) +{ + struct lcr_list *node = NULL; + size_t buf_len = 0; + char *buf = NULL; + char *options = NULL; + int ret; + char *type = NULL; + + if (is_mount_options_invalid(mount)) { + ERROR("oci container mounts element(type) is empty"); + return NULL; + } + type = mount->type; + if (is_mount_type_sysfs(type)) { + type = "sys"; + } + + options = get_mount_readmode_options(mount, type); + if (options == NULL) { + ERROR("Failed to trans to lxc options"); + goto out_free; + } + + buf_len = strlen(type) + strlen(options) + 2; + buf = calloc(buf_len, 1); + if (buf == NULL) { + DEBUG("Out of memory"); + goto out_free; + } + + ret = sprintf_s(buf, buf_len, "%s:%s", type, options); + if (ret < 0) { + DEBUG("Failed to print string"); + goto out_free; + } + node = create_lcr_list_node("lxc.mount.auto", buf); + +out_free: + free(options); + free(buf); + return node; +} + +/* trans mount entry to lxc */ +static struct lcr_list *trans_mount_entry_to_lxc(const defs_mount *mount) +{ + struct lcr_list *node = NULL; + size_t buf_len = 0; + char *buf = NULL; + char *options = NULL; + char *replaced_dest = NULL; + int ret; + + char *replaced_source = util_string_replace(" ", SPACE_MAGIC_STR, mount->source); + if (replaced_source == NULL) { + ERROR("memory allocation error"); + goto err_out; + } + replaced_dest = util_string_replace(" ", SPACE_MAGIC_STR, mount->destination); + if (replaced_dest == NULL) { + ERROR("memory allocation error"); + free(replaced_source); + goto err_out; + } + + options = trans_mount_to_lxc_options(mount); + if (options == NULL) { + ERROR("Failed to trans to lxc options"); + goto out_free; + } + + buf_len = strlen(replaced_dest) + strlen(mount->type) + strlen(replaced_source) + strlen(options) + 8; + buf = calloc(buf_len, 1); + if (buf == NULL) { + ERROR("Out of memory"); + goto out_free; + } + + ret = sprintf_s(buf, buf_len, "%s %s %s %s 0 0", replaced_source, replaced_dest + 1, mount->type, options); + if (ret < 0) { + ERROR("Failed to print string"); + goto out_free; + } + node = create_lcr_list_node("lxc.mount.entry", buf); + +out_free: + free(options); + free(buf); + free(replaced_source); + free(replaced_dest); +err_out: + return node; +} + +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 struct lcr_list *trans_oci_mounts_normal(const defs_mount *tmp) +{ + struct lcr_list *node = NULL; + if (is_mount_type_cgroup(tmp->type) || is_mount_type_proc(tmp->type) || is_mount_type_sysfs(tmp->type)) { + node = trans_mount_auto_to_lxc(tmp); + } else { + node = trans_mount_entry_to_lxc(tmp); + } + + return node; +} + +static struct lcr_list *trans_oci_mounts_system_container(const defs_mount *tmp) +{ + struct lcr_list *node = NULL; + if (is_mount_type_cgroup(tmp->type) || (is_mount_type_proc(tmp->source) && is_mount_type_proc(tmp->type)) || + is_mount_type_sysfs(tmp->type)) { + node = trans_mount_auto_to_lxc(tmp); + } else { + node = trans_mount_entry_to_lxc(tmp); + } + + return node; +} + +static struct lcr_list *trans_oci_mounts_node(const oci_runtime_spec *c, const defs_mount *tmp) +{ + struct lcr_list *node = NULL; + // system container + if (is_system_container(c)) { + node = trans_oci_mounts_system_container(tmp); + } else { + node = trans_oci_mounts_normal(tmp); + } + + return node; +} + +static inline bool is_mount_destination_dev(const char *destination) +{ + return destination != NULL && strcmp(destination, "/dev") == 0; +} + +static inline bool should_ignore_dev_mount(const defs_mount *tmp, bool system_container) +{ + return system_container && is_mount_destination_dev(tmp->destination); +} + +/* trans oci mounts */ +struct lcr_list *trans_oci_mounts(const oci_runtime_spec *c) +{ + struct lcr_list *conf = NULL; + struct lcr_list *node = NULL; + defs_mount *tmp = NULL; + size_t i; + bool system_container = is_system_container(c); + + conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + for (i = 0; i < c->mounts_len; i++) { + tmp = c->mounts[i]; + if (tmp->type == NULL) { + goto out_free; + } + + if (should_ignore_dev_mount(tmp, system_container)) { + continue; + } + node = trans_oci_mounts_node(c, tmp); + if (node == NULL) { + goto out_free; + } + lcr_list_add_tail(conf, node); + } + + return conf; +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +static int trans_one_oci_id_mapping(struct lcr_list *conf, const char *typ, const defs_id_mapping *id, const char *path) +{ + int nret; + struct lcr_list *node = NULL; + char buf_value[300] = { 0 }; + char subid[ID_MAP_LEN] = { 0 }; + + nret = sprintf_s(buf_value, sizeof(buf_value), "%s %u %u %u", typ, id->container_id, id->host_id, id->size); + if (nret < 0) { + return -1; + } + + node = create_lcr_list_node("lxc.idmap", buf_value); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + + nret = sprintf_s(subid, sizeof(subid), "%u:%u:%u", id->container_id, id->host_id, id->size); + if (nret < 0) { + return -1; + } + nret = util_atomic_write_file(path, subid); + if (nret < 0) { + return -1; + } + return 0; +} + +static int trans_oci_uid_mapping(struct lcr_list *conf, defs_id_mapping **uid_mappings, size_t uid_mappings_len) +{ + size_t i; + + for (i = 0; uid_mappings != NULL && i < uid_mappings_len; i++) { + int nret = trans_one_oci_id_mapping(conf, "u", uid_mappings[i], SUB_UID_PATH); + if (nret < 0) { + return nret; + } + } + return 0; +} + +static int trans_oci_gid_mapping(struct lcr_list *conf, defs_id_mapping **gid_mappings, size_t gid_mappings_len) +{ + size_t i; + + for (i = 0; gid_mappings != NULL && i < gid_mappings_len; i++) { + int nret = trans_one_oci_id_mapping(conf, "g", gid_mappings[i], SUB_GID_PATH); + if (nret < 0) { + return nret; + } + } + return 0; +} + +/* trans oci id mapping */ +static struct lcr_list *trans_oci_id_mapping(const oci_runtime_config_linux *l) +{ + struct lcr_list *conf = NULL; + int nret = 0; + + conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + nret = trans_oci_uid_mapping(conf, l->uid_mappings, l->uid_mappings_len); + if (nret < 0) { + goto out_free; + } + + nret = trans_oci_gid_mapping(conf, l->gid_mappings, l->gid_mappings_len); + if (nret < 0) { + goto out_free; + } + + return conf; + +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +#define WILDCARD (-1LL) + +static int trans_conf_int(struct lcr_list *conf, const char *lxc_key, int val) +{ + struct lcr_list *node = NULL; + char buf_value[300] = { 0 }; + int nret; + + nret = sprintf_s(buf_value, sizeof(buf_value), "%d", val); + if (nret < 0) { + return -1; + } + node = create_lcr_list_node(lxc_key, buf_value); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + return 0; +} + +static int trans_conf_uint32(struct lcr_list *conf, const char *lxc_key, uint32_t val) +{ + struct lcr_list *node = NULL; + char buf_value[300] = { 0 }; + int nret; + + nret = sprintf_s(buf_value, sizeof(buf_value), "%u", (unsigned int)val); + if (nret < 0) { + return -1; + } + node = create_lcr_list_node(lxc_key, buf_value); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + return 0; +} + +static int trans_conf_int64(struct lcr_list *conf, const char *lxc_key, int64_t val) +{ + struct lcr_list *node = NULL; + char buf_value[300] = { 0 }; + int nret; + + nret = sprintf_s(buf_value, sizeof(buf_value), "%lld", (long long)val); + if (nret < 0) { + return -1; + } + node = create_lcr_list_node(lxc_key, buf_value); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + return 0; +} + +static int trans_conf_uint64(struct lcr_list *conf, const char *lxc_key, uint64_t val) +{ + struct lcr_list *node = NULL; + char buf_value[300] = { 0 }; + int nret; + + nret = sprintf_s(buf_value, sizeof(buf_value), "%llu", (unsigned long long)val); + if (nret < 0) { + return -1; + } + node = create_lcr_list_node(lxc_key, buf_value); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + return 0; +} + +/* trans resources mem swap */ +static int trans_resources_mem_swap(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + int nret; + + if (res->memory->reservation != INVALID_INT) { + /* set soft limit of memory usage */ + nret = trans_conf_int64(conf, "lxc.cgroup.memory.soft_limit_in_bytes", res->memory->reservation); + if (nret < 0) { + goto out; + } + } + if (res->memory->swap != INVALID_INT) { + /* set limit of memory+swap usage */ + nret = trans_conf_int64(conf, "lxc.cgroup.memory.memsw.limit_in_bytes", res->memory->swap); + if (nret < 0) { + goto out; + } + } + + if (res->memory->swappiness != INVALID_INT) { + /* set swappiness parameter of vmscan */ + nret = trans_conf_uint64(conf, "lxc.cgroup.memory.swappiness", res->memory->swappiness); + if (nret < 0) { + goto out; + } + } + ret = 0; +out: + return ret; +} + +static int trans_resources_mem_limit(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + if (res->memory->limit != INVALID_INT) { + /* set limit of memory usage */ + int nret = trans_conf_int64(conf, "lxc.cgroup.memory.limit_in_bytes", res->memory->limit); + if (nret < 0) { + return -1; + } + } + return 0; +} + +/* trans resources mem kernel */ +static int trans_resources_mem_kernel(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + int nret; + + if (res->memory->kernel != INVALID_INT) { + /* set hard limit for kernel memory */ + nret = trans_conf_int64(conf, "lxc.cgroup.memory.kmem.limit_in_bytes", res->memory->kernel); + if (nret < 0) { + goto out; + } + } + if (res->memory->kernel_tcp != INVALID_INT) { + /* set hard limit for tcp buf memory */ + nret = trans_conf_int64(conf, "lxc.cgroup.memory.kmem.tcp.limit_in_bytes", res->memory->kernel_tcp); + if (nret < 0) { + goto out; + } + } + ret = 0; +out: + return ret; +} + +static int trans_resources_mem_disable_oom(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + if (res->memory->disable_oom_killer) { + node = create_lcr_list_node("lxc.cgroup.memory.oom_control", "1"); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + } + return 0; +} + +/* trans resources memory */ +static int trans_resources_memory(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + + if (res->memory == NULL) { + return 0; + } + + if (trans_resources_mem_limit(res, conf) != 0) { + goto out; + } + + if (trans_resources_mem_swap(res, conf) != 0) { + goto out; + } + + if (trans_resources_mem_kernel(res, conf) != 0) { + goto out; + } + + if (trans_resources_mem_disable_oom(res, conf) != 0) { + goto out; + } + ret = 0; +out: + return ret; +} + +static int trans_resources_devices_node(const oci_runtime_defs_linux_device_cgroup *lrd, struct lcr_list *conf, + const char *buf_value) +{ + struct lcr_list *node = NULL; + int ret = -1; + + if (lrd->allow == true) { + node = create_lcr_list_node("lxc.cgroup.devices.allow", buf_value); + } else { + node = create_lcr_list_node("lxc.cgroup.devices.deny", buf_value); + } + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + + ret = 0; +out: + return ret; +} + +static int trans_resources_devices_no_match(const oci_runtime_defs_linux_device_cgroup *lrd, char *buf_value, + size_t size) +{ + int ret = 0; + if (lrd->minor != WILDCARD) { + ret = sprintf_s(buf_value, size, "%s %lld:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->major), + (long long)lrd->minor, lrd->access ? lrd->access : "rwm"); + } else { + ret = sprintf_s(buf_value, size, "%s %lld:* %s", lrd->type ? lrd->type : "a", (long long)(lrd->major), + lrd->access ? lrd->access : "rwm"); + } + + return ret; +} + +static int trans_resources_devices_match(const oci_runtime_defs_linux_device_cgroup *lrd, char *buf_value, size_t size) +{ + int ret = 0; + if (lrd->minor != WILDCARD) { + ret = sprintf_s(buf_value, size, "%s *:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->minor), + lrd->access ? lrd->access : "rwm"); + } else { + ret = sprintf_s(buf_value, size, "%s *:* %s", lrd->type ? lrd->type : "a", lrd->access ? lrd->access : "rwm"); + } + + return ret; +} + +static int trans_resources_devices_ret(const oci_runtime_defs_linux_device_cgroup *lrd, char *buf_value, size_t size) +{ + int ret = 0; + if (lrd->major != WILDCARD) { + ret = trans_resources_devices_no_match(lrd, buf_value, size); + } else { + ret = trans_resources_devices_match(lrd, buf_value, size); + } + + return ret; +} + +/* trans resources devices */ +static int trans_resources_devices(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + size_t i = 0; + char buf_value[300] = { 0 }; + + for (i = 0; i < res->devices_len; i++) { + oci_runtime_defs_linux_device_cgroup *lrd = res->devices[i]; + if (trans_resources_devices_ret(lrd, buf_value, sizeof(buf_value)) < 0) { + goto out; + } + + if (trans_resources_devices_node(lrd, conf, buf_value) < 0) { + goto out; + } + } + ret = 0; +out: + return ret; +} + +/* trans resources cpu cfs */ +static int trans_resources_cpu_cfs(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + + if (res->cpu->quota != INVALID_INT) { + if (trans_conf_int64(conf, "lxc.cgroup.cpu.cfs_quota_us", res->cpu->quota) < 0) { + goto out; + } + } + if (res->cpu->period != INVALID_INT) { + if (trans_conf_uint64(conf, "lxc.cgroup.cpu.cfs_period_us", res->cpu->period) < 0) { + goto out; + } + } + ret = 0; +out: + return ret; +} + +/* trans resources cpu rt */ +static int trans_resources_cpu_rt(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + + if (res->cpu->realtime_runtime != INVALID_INT) { + if (trans_conf_int64(conf, "lxc.cgroup.cpu.rt_runtime_us", res->cpu->realtime_runtime) < 0) { + goto out; + } + } + if (res->cpu->realtime_period != INVALID_INT) { + if (trans_conf_uint64(conf, "lxc.cgroup.cpu.rt_period_us", res->cpu->realtime_period) < 0) { + goto out; + } + } + ret = 0; +out: + return ret; +} + +/* trans resources cpu set */ +static int trans_resources_cpu_set(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + + if (res->cpu->cpus != NULL) { + node = create_lcr_list_node("lxc.cgroup.cpuset.cpus", res->cpu->cpus); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + if (res->cpu->mems != NULL) { + node = create_lcr_list_node("lxc.cgroup.cpuset.mems", res->cpu->mems); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + ret = 0; +out: + return ret; +} + +/* trans resources cpu shares */ +static int trans_resources_cpu_shares(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + if (res->cpu->shares != INVALID_INT) { + int nret = trans_conf_int64(conf, "lxc.cgroup.cpu.shares", (int64_t)(res->cpu->shares)); + if (nret < 0) { + return -1; + } + } + return 0; +} + +/* trans resources cpu */ +static int trans_resources_cpu(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + + if (res->cpu == NULL) { + return 0; + } + + if (trans_resources_cpu_cfs(res, conf)) { + goto out; + } + + if (trans_resources_cpu_rt(res, conf)) { + goto out; + } + + if (trans_resources_cpu_set(res, conf)) { + goto out; + } + + if (trans_resources_cpu_shares(res, conf)) { + goto out; + } + + ret = 0; + +out: + return ret; +} + +/* trans resources blkio weight */ +static int trans_blkio_weight(const oci_runtime_config_linux_resources_block_io *block_io, struct lcr_list *conf) +{ + int ret = -1; + + if (block_io->weight != INVALID_INT) { + if (trans_conf_int(conf, "lxc.cgroup.blkio.weight", block_io->weight) < 0) { + goto out; + } + } + if (block_io->leaf_weight != INVALID_INT) { + if (trans_conf_int(conf, "lxc.cgroup.blkio.leaf_weight", block_io->leaf_weight) < 0) { + goto out; + } + } + ret = 0; + +out: + return ret; +} + +/* trans resources blkio wdevice */ +static int trans_blkio_wdevice(const oci_runtime_config_linux_resources_block_io *block_io, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + size_t i = 0; + char buf_value[300] = { 0 }; + + for (i = 0; i < block_io->weight_device_len; i++) { + int nret; + oci_runtime_defs_linux_block_io_device_weight *wd = block_io->weight_device[i]; + if ((wd != NULL) && wd->weight != INVALID_INT) { + nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), (long long)wd->minor, + wd->weight); + if (nret < 0) { + goto out; + } + + node = create_lcr_list_node("lxc.cgroup.blkio.weight_device", buf_value); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + if ((wd != NULL) && wd->leaf_weight != INVALID_INT) { + nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), + (long long)(wd->minor), wd->leaf_weight); + if (nret < 0) { + goto out; + } + + node = create_lcr_list_node("lxc.cgroup.blkio.leaf_weight_device", buf_value); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + } + ret = 0; +out: + return ret; +} + +/* trans resources blkio throttle */ +static int trans_blkio_throttle(oci_runtime_defs_linux_block_io_device_throttle **throttle, size_t len, + const char *lxc_key, struct lcr_list *conf) +{ + struct lcr_list *node = NULL; + int ret = -1; + size_t i; + + if ((throttle == NULL) || len == 0) { + return 0; + } + + for (i = 0; i < len; i++) { + if (throttle[i] && throttle[i]->rate != INVALID_INT) { + int nret; + char buf_value[300] = { 0x00 }; + nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %llu", (long long)throttle[i]->major, + (long long)(throttle[i]->minor), (unsigned long long)(throttle[i]->rate)); + if (nret < 0) { + goto out; + } + + node = create_lcr_list_node(lxc_key, buf_value); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + } + ret = 0; +out: + return ret; +} + +/* trans resources blkio */ +static int trans_resources_blkio(const oci_runtime_config_linux_resources_block_io *block_io, struct lcr_list *conf) +{ + int ret = -1; + + if (block_io == NULL) { + return 0; + } + + if (trans_blkio_weight(block_io, conf)) { + goto out; + } + + if (trans_blkio_wdevice(block_io, conf)) { + goto out; + } + + if (trans_blkio_throttle(block_io->throttle_read_bps_device, block_io->throttle_read_bps_device_len, + "lxc.cgroup.blkio.throttle.read_bps_device", conf)) { + goto out; + } + + if (trans_blkio_throttle(block_io->throttle_write_bps_device, block_io->throttle_write_bps_device_len, + "lxc.cgroup.blkio.throttle.write_bps_device", conf)) { + goto out; + } + + if (trans_blkio_throttle(block_io->throttle_read_iops_device, block_io->throttle_read_iops_device_len, + "lxc.cgroup.blkio.throttle.read_iops_device", conf)) { + goto out; + } + + if (trans_blkio_throttle(block_io->throttle_write_iops_device, block_io->throttle_write_iops_device_len, + "lxc.cgroup.blkio.throttle.write_iops_device", conf)) { + goto out; + } + + ret = 0; +out: + return ret; +} + +/* trans resources hugetlb */ +static int trans_resources_hugetlb(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + size_t i = 0; + char buf_key[300] = { 0 }; + + for (i = 0; i < res->hugepage_limits_len; i++) { + oci_runtime_config_linux_resources_hugepage_limits_element *lrhl = res->hugepage_limits[i]; + if (lrhl->page_size != NULL) { + if (sprintf_s(buf_key, sizeof(buf_key), "lxc.cgroup.hugetlb.%s.limit_in_bytes", lrhl->page_size) < 0) { + goto out; + } + + if (trans_conf_uint64(conf, buf_key, lrhl->limit) < 0) { + return -1; + } + } + } + + ret = 0; +out: + return ret; +} + +/* trans resources network */ +static int trans_resources_network(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + size_t i = 0; + char buf_value[300] = { 0 }; + + if (!res->network) { + return 0; + } + + if (res->network->class_id != INVALID_INT) { + if (trans_conf_uint32(conf, "lxc.cgroup.net_cls.classid", res->network->class_id) < 0) { + return -1; + } + } + + for (i = 0; i < res->network->priorities_len; i++) { + oci_runtime_defs_linux_network_interface_priority *lrnp = res->network->priorities[i]; + if ((lrnp != NULL) && lrnp->name != NULL && lrnp->priority != INVALID_INT) { + if (sprintf_s(buf_value, sizeof(buf_value), "%s %u", lrnp->name, lrnp->priority) < 0) { + goto out; + } + + struct lcr_list *node = create_lcr_list_node("lxc.cgroup.net_prio.ifpriomap", buf_value); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + } + + ret = 0; +out: + return ret; +} + +/* trans resources pids */ +static int trans_resources_pids(const oci_runtime_config_linux_resources *res, struct lcr_list *conf) +{ + int ret = -1; + char buf_value[300] = { 0 }; + + if (res->pids == NULL) { + return 0; + } + + if (res->pids->limit != INVALID_INT) { + int nret; + if (res->pids->limit == -1) { + nret = sprintf_s(buf_value, sizeof(buf_value), "max"); + } else { + nret = sprintf_s(buf_value, sizeof(buf_value), "%lld", (long long)(res->pids->limit)); + } + if (nret < 0) { + goto out; + } + + struct lcr_list *node = create_lcr_list_node("lxc.cgroup.pids.max", buf_value); + if (node == NULL) { + goto out; + } + lcr_list_add_tail(conf, node); + } + + ret = 0; +out: + return ret; +} + +/* trans oci resources */ +static struct lcr_list *trans_oci_resources(const oci_runtime_config_linux_resources *res) +{ + struct lcr_list *conf = NULL; + + conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + if (trans_resources_devices(res, conf)) { + goto out_free; + } + + if (trans_resources_memory(res, conf)) { + goto out_free; + } + + if (trans_resources_cpu(res, conf)) { + goto out_free; + } + + if (trans_resources_blkio(res->block_io, conf)) { + goto out_free; + } + + if (trans_resources_hugetlb(res, conf)) { + goto out_free; + } + + if (trans_resources_network(res, conf)) { + goto out_free; + } + + if (trans_resources_pids(res, conf)) { + goto out_free; + } + + return conf; + +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +struct namespace_map_def { + char *ns_name; + char *lxc_name; +}; + +static char *trans_oci_namespace_to_lxc(const char *typ) +{ + struct namespace_map_def namespaces_map[] = { + { "pid", "lxc.namespace.share.pid" }, { "network", "lxc.namespace.share.net" }, + { "ipc", "lxc.namespace.share.ipc" }, { "uts", "lxc.namespace.share.uts" }, + { "mount", "lxc.namespace.share.mnt" }, { "user", "lxc.namespace.share.user" }, + { "cgroup", "lxc.namespace.share.cgroup" }, { NULL, NULL } + }; + const struct namespace_map_def *p = NULL; + + for (p = namespaces_map; p != NULL && p->ns_name != NULL; p++) { + if (strcmp(typ, p->ns_name) == 0) { + return util_strdup_s(p->lxc_name); + } + } + return NULL; +} + +/* trans oci namespaces */ +static struct lcr_list *trans_oci_namespaces(const oci_runtime_config_linux *l) +{ + struct lcr_list *conf = NULL; + struct lcr_list *node = NULL; + size_t i; + oci_runtime_defs_linux_namespace_reference *ns = NULL; + + conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + for (i = 0; i < l->namespaces_len; i++) { + char *ns_name = NULL; + ns = l->namespaces[i]; + + if (ns->type == NULL || ns->path == NULL) { + continue; + } + + ns_name = trans_oci_namespace_to_lxc(ns->type); + if (ns_name == NULL) { + continue; + } + + node = create_lcr_list_node(ns_name, ns->path); + free(ns_name); + if (node == NULL) { + goto out_free; + } + lcr_list_add_tail(conf, node); + } + + return conf; + +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +/* trans oci mask ro paths */ +static struct lcr_list *trans_oci_mask_ro_paths(const oci_runtime_config_linux *l) +{ + struct lcr_list *conf = NULL; + struct lcr_list *node = NULL; + size_t i; + char *path = NULL; + + conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + for (i = 0; i < l->masked_paths_len; i++) { + path = l->masked_paths[i]; + if (path == NULL) { + continue; + } + node = create_lcr_list_node("lxc.isulad.rootfs.maskedpaths", path); + if (node == NULL) { + goto out_free; + } + lcr_list_add_tail(conf, node); + } + + for (i = 0; i < l->readonly_paths_len; i++) { + path = l->readonly_paths[i]; + if (path == NULL) { + continue; + } + node = create_lcr_list_node("lxc.isulad.rootfs.ropaths", path); + if (node == NULL) { + goto out_free; + } + lcr_list_add_tail(conf, node); + } + + return conf; + +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +#define POPULATE_DEVICE_SIZE (300 + PATH_MAX) +/* trans oci linux devices */ +static struct lcr_list *trans_oci_linux_devices(const oci_runtime_config_linux *l) +{ + struct lcr_list *conf = NULL; + struct lcr_list *node = NULL; + size_t i = 0; + int nret = 0; + oci_runtime_defs_linux_device *device = NULL; + char buf_value[POPULATE_DEVICE_SIZE] = { 0 }; + + conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + for (i = 0; i < l->devices_len; i++) { + device = l->devices[i]; + + if (device->type == NULL || device->path == NULL) { + continue; + } + + /* lxc.populate_device = PATH_IN_CONTAINER:DEVICETYPE:MAJOR:MINOR:MODE:UID:GID + * For e.g. lxc.populate_device = /dev/sda:b:8:0:0666:0:0 + */ + nret = sprintf_s(buf_value, sizeof(buf_value), "%s:%s:%lld:%lld:%d:%u:%u", device->path, device->type, + (long long int)(device->major), (long long int)(device->minor), device->file_mode, device->uid, + device->gid); + if (nret < 0) { + ERROR("Failed to get populate device string"); + goto out_free; + } + + node = create_lcr_list_node("lxc.isulad.populate.device", buf_value); + if (node == NULL) { + goto out_free; + } + lcr_list_add_tail(conf, node); + } + + return conf; + +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +static inline bool is_seccomp_action_kill(const char *value) +{ + return strcmp(value, "SCMP_ACT_KILL") == 0; +} + +static inline bool is_seccomp_action_trap(const char *value) +{ + return strcmp(value, "SCMP_ACT_TRAP") == 0; +} + +static inline bool is_seccomp_action_allow(const char *value) +{ + return strcmp(value, "SCMP_ACT_ALLOW") == 0; +} + +static inline bool is_seccomp_action_trace(const char *value) +{ + return strcmp(value, "SCMP_ACT_TRACE") == 0; +} + +static inline bool is_seccomp_action_errno(const char *value) +{ + return strcmp(value, "SCMP_ACT_ERRNO") == 0; +} + +/* seccomp trans action */ +static char *seccomp_trans_action(const char *action) +{ + if (is_seccomp_action_kill(action)) { + return util_strdup_s("kill"); + } else if (is_seccomp_action_trap(action)) { + return util_strdup_s("trap"); + } else if (is_seccomp_action_allow(action)) { + return util_strdup_s("allow"); + } else if (is_seccomp_action_trace(action)) { + return util_strdup_s("trace 1"); + } else if (is_seccomp_action_errno(action)) { + return util_strdup_s("errno 1"); + } + + return NULL; +} + +static bool is_action_allow(const char *value) +{ + return strcmp(value, "allow") == 0; +} + +#define DEFAULT_ACTION_OFFSET 12 +/* seccomp append head info */ +static int seccomp_append_head_info(const char *action, Buffer *buffer) +{ + int ret = 0; + char *default_action = NULL; + + if (action == NULL) { + return -1; + } + + default_action = seccomp_trans_action(action); + if (default_action == NULL) { + ERROR("Failed to translate seccomp action"); + return -1; + } + + if (is_action_allow(default_action)) { + ret = buffer_nappendf(buffer, strlen(default_action) + DEFAULT_ACTION_OFFSET, "blacklist %s\n", default_action); + } else { + ret = buffer_nappendf(buffer, strlen(default_action) + DEFAULT_ACTION_OFFSET, "whitelist %s\n", default_action); + } + if (ret != 0) { + ERROR("Failed to append seccomp config head info\n"); + ret = -1; + goto out; + } + +out: + free(default_action); + return ret; +} + +/* get hostarch */ +static char *get_hostarch(void) +{ + struct utsname uts; + size_t len; + size_t i; + /* no x32 kernels */ + lcr_host_arch arch_type[] = { + { "i686", "[x86]", 4 }, { "x32", "[x32]", 3 }, { "x86_64", "[x86_64]", 6 }, + { "armv7", "[arm]", 5 }, { "aarch64", "[arm64]", 7 }, { "ppc64le", "[ppc64le]", 7 }, + { "ppc64", "[ppc64]", 5 }, { "ppc", "[ppc]", 3 }, { "mips64n32", "[mips64n32]", 9 }, + { "mips64", "[mips64]", 6 }, { "mips", "[mips]", 4 }, { "s390x", "[s390x]", 5 }, + { "s390", "[s390]", 4 }, { "parisc64", "[parisc64]", 8 }, { "parisc", "[parisc]", 6 }, + }; + + if (uname(&uts) < 0) { + SYSERROR("Failed to read host arch"); + return NULL; + } + len = sizeof(arch_type) / sizeof(lcr_host_arch); + for (i = 0; i < len; i++) { + if (i == 0 || i == 1 || i == 2) { + if (strcmp(uts.machine, arch_type[i].arch) == 0) { + return util_strdup_s(arch_type[i].value); + } + } else { + if (strncmp(uts.machine, arch_type[i].arch, (size_t)(arch_type[i].num)) == 0) { + return util_strdup_s(arch_type[i].value); + } + } + } + + ERROR("Failed to get machine type"); + return NULL; +} + +/* seccomp trans arch */ +static char *seccomp_trans_arch(const char *arch) +{ + lcr_arch_value arch_type[] = { + { "SCMP_ARCH_X86", "[x86]" }, + { "SCMP_ARCH_X86_64", "[x86_64]" }, + { "SCMP_ARCH_X32", "[x32]" }, + { "SCMP_ARCH_ARM", "[arm]" }, + { "SCMP_ARCH_AARCH64", "[arm64]" }, + { "SCMP_ARCH_MIPS", "[mips]" }, + { "SCMP_ARCH_MIPS64", "[mips64]" }, + { "SCMP_ARCH_MIPS64N32", "[mips64n32]" }, + { "SCMP_ARCH_MIPSEL", "[mipsel]" }, + { "SCMP_ARCH_MIPSEL64", "[mipsel64]" }, + { "SCMP_ARCH_MIPSEL64N32", "[mipsel64n32]" }, + { "SCMP_ARCH_PPC", "[ppc]" }, + { "SCMP_ARCH_PPC64", "[ppc64]" }, + { "SCMP_ARCH_PPC64LE", "[ppc64le]" }, + { "SCMP_ARCH_S390", "[s390]" }, + { "SCMP_ARCH_S390X", "[s390x]" }, + { "SCMP_ARCH_PARISC", "[parisc]" }, + { "SCMP_ARCH_PARISC64", "[parisc64]" }, + { "SCMP_ARCH_ALL", "[all]" }, + { "SCMP_ARCH_AUTO", "" }, + }; + size_t len; + size_t i = 0; + len = sizeof(arch_type) / sizeof(lcr_arch_value); + for (i = 0; i < len; i++) { + if (strcmp(arch_type[i].arch, "SCMP_ARCH_AUTO") == 0) { + return get_hostarch(); + } else if (strcmp(arch, arch_type[i].arch) == 0) { + return util_strdup_s(arch_type[i].value); + } + } + return NULL; +} + +/* seccomp append arch */ +static int seccomp_append_arch(char *arch, Buffer *buffer) +{ + int ret = 0; + char *trans_arch = NULL; + + if (arch == NULL) { + return -1; + } + + trans_arch = seccomp_trans_arch(arch); + if (trans_arch == NULL) { + ERROR("Failed to translate seccomp arch: %s", arch); + return -1; + } + + if (buffer_nappendf(buffer, strlen(trans_arch) + 2, "%s\n", trans_arch)) { + ERROR("Failed to append seccomp config head info\n"); + ret = -1; + } + + free(trans_arch); + return ret; +} + +/* seccomp append rule */ +static int seccomp_append_rule(const oci_runtime_defs_linux_syscall *syscall, size_t i, Buffer *buffer, char *action) +{ + int ret = 0; + size_t j = 0; + + if (syscall->names[i] == NULL) { + ERROR("Failed to get syscall name"); + ret = -1; + goto out; + } + if (buffer_nappendf(buffer, strlen(syscall->names[i]) + strlen(action) + 2, "%s %s", syscall->names[i], action)) { + ERROR("Failed to append syscall name and action\n"); + ret = -1; + goto out; + } + + for (j = 0; j < syscall->args_len; j++) { + if ((syscall->args[j] == NULL) || (syscall->args[j]->op == NULL)) { + ERROR("Failed to get syscall args"); + ret = -1; + goto out; + } + if (buffer_nappendf(buffer, 20 * 3 + strlen(syscall->args[j]->op), " [%u,%llu,%s,%llu]", + syscall->args[j]->index, syscall->args[j]->value, syscall->args[j]->op, + syscall->args[j]->value_two)) { + ERROR("Failed to append syscall rules\n"); + ret = -1; + goto out; + } + } + + if (buffer_nappendf(buffer, 2, "\n")) { + ERROR("Failed to append newline\n"); + ret = -1; + goto out; + } +out: + return ret; +} + +/* seccomp append rules */ +static int seccomp_append_rules(const oci_runtime_defs_linux_syscall *syscall, Buffer *buffer) +{ + int ret = 0; + size_t i = 0; + char *action = NULL; + + if (syscall == NULL) { + return -1; + } + + if ((syscall->action == NULL) || syscall->names_len == 0) { + return -1; + } + + action = seccomp_trans_action(syscall->action); + if (action == NULL) { + ERROR("Failed to translate action"); + ret = -1; + goto out; + } + + for (i = 0; i < syscall->names_len; i++) { + if (seccomp_append_rule(syscall, i, buffer, action)) { + ret = -1; + goto out; + } + } +out: + free(action); + return ret; +} + +static struct lcr_list *trans_oci_linux_sysctl(const json_map_string_string *sysctl) +{ + struct lcr_list *conf = NULL; + struct lcr_list *node = NULL; + size_t i; + + conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + for (i = 0; i < sysctl->len; i++) { + char sysk[BUFSIZ] = { 0 }; + int nret = sprintf_s(sysk, sizeof(sysk), "lxc.sysctl.%s", sysctl->keys[i]); + if (nret < 0) { + ERROR("Failed to print string"); + goto out_free; + } + node = create_lcr_list_node(sysk, sysctl->values[i]); + if (node == NULL) { + goto out_free; + } + lcr_list_add_tail(conf, node); + } + + return conf; + +out_free: + lcr_free_config(conf); + free(conf); + return NULL; +} + +static int append_seccomp_with_archs(const oci_runtime_config_linux_seccomp *seccomp, Buffer *buffer) +{ + int ret = 0; + size_t i = 0; + size_t j = 0; + + for (i = 0; i < seccomp->architectures_len; i++) { + if (seccomp_append_arch(seccomp->architectures[i], buffer)) { + ret = -1; + goto out; + } + /* append rules*/ + for (j = 0; j < seccomp->syscalls_len; j++) { + if (seccomp_append_rules(seccomp->syscalls[j], buffer)) { + ret = -1; + goto out; + } + } + } +out: + return ret; +} + +/* lxc seccomp conf format: + To support limit syscall arguments, extend the version 2 file to the following format: + + syscall_name action [index,value,op,valueTwo] [index,value,op]... + for one arguments, [index,value,valueTwo,op] + + index: the index for syscall arguments (type uint) + + value: the value for syscall arguments (type uint64) + + op: the operator for syscall arguments(string), a valid list of constants as of libseccomp v2.3.2 is + SCMP_CMP_NE,SCMP_CMP_LE,SCMP_CMP_LE, SCMP_CMP_EQ, SCMP_CMP_GE, + SCMP_CMP_GT, SCMP_CMP_MASKED_EQ, or !=,<=,==,>=,>,&= + + valueTwo: the value for syscall arguments only used for mask eq (type uint64, optional) + +For example: + + 2 + blacklist allow + reject_force_umount # comment this to allow umount -f; not recommended + [all] + kexec_load errno 1 [0,1,SCMP_CMP_LE][3,1,==][5,1,SCMP_CMP_MASKED_EQ,1] + open_by_handle_at errno 1 + init_module errno 1 + finit_module errno 1 + delete_module errno 1 + +*/ +static int trans_oci_seccomp(const oci_runtime_config_linux_seccomp *seccomp, char **seccomp_conf) +{ + int ret = 0; + size_t j = 0; + size_t init_size = 4 * SIZE_KB; + + Buffer *buffer = buffer_alloc(init_size); + if (buffer == NULL) { + ERROR("Failed to malloc output_buffer\n"); + return -1; + } + + /* config version */ + if (buffer_nappendf(buffer, 3, "2\n")) { + ERROR("Failed to append seccomp config version\n"); + ret = -1; + goto out_free; + } + + /* append head info */ + if (seccomp_append_head_info(seccomp->default_action, buffer)) { + ret = -1; + goto out_free; + } + + /* append architectures */ + if (seccomp->architectures_len != 0) { + ret = append_seccomp_with_archs(seccomp, buffer); + if (ret != 0) { + goto out_free; + } + } else { + // add rules directly(eg: blacklist) + for (j = 0; j < seccomp->syscalls_len; j++) { + if (seccomp_append_rules(seccomp->syscalls[j], buffer)) { + ret = -1; + goto out_free; + } + } + } + *seccomp_conf = buffer_to_s(buffer); + if (*seccomp_conf == NULL) { + ret = -1; + goto out_free; + } + +out_free: + buffer_free(buffer); + return ret; +} + +/* trans oci linux */ +struct lcr_list *trans_oci_linux(const oci_runtime_config_linux *l, char **seccomp_conf) +{ + int ret = 0; + struct lcr_list *tmp = NULL; + + struct lcr_list *conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + // UID/GID Mapping + tmp = trans_oci_id_mapping(l); + if (tmp == NULL) { + goto out_free; + } + lcr_list_merge(conf, tmp); + + // Resources + if (l->resources != NULL) { + tmp = trans_oci_resources(l->resources); + if (tmp == NULL) { + goto out_free; + } + lcr_list_merge(conf, tmp); + } + + // linux devices + tmp = trans_oci_linux_devices(l); + if (tmp == NULL) { + goto out_free; + } + lcr_list_merge(conf, tmp); + + // Namespaces + tmp = trans_oci_namespaces(l); + if (tmp == NULL) { + goto out_free; + } + lcr_list_merge(conf, tmp); + + // MaskedPaths and ReadonlyPaths + tmp = trans_oci_mask_ro_paths(l); + if (tmp == NULL) { + goto out_free; + } + lcr_list_merge(conf, tmp); + + // sysctl + if (l->sysctl != NULL && l->uid_mappings == NULL && l->gid_mappings == NULL) { + tmp = trans_oci_linux_sysctl(l->sysctl); + if (tmp == NULL) { + goto out_free; + } + lcr_list_merge(conf, tmp); + } + + // seccomp + if (l->seccomp != NULL && seccomp_conf != NULL) { + ret = trans_oci_seccomp(l->seccomp, seccomp_conf); + if (ret) { + goto out_free; + } + } + + return conf; +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +/* trans annotations */ +struct lcr_list *trans_annotations(const json_map_string_string *anno) +{ + size_t i, j; + size_t len; + int ret = 0; + + struct lcr_list *conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + len = sizeof(g_require_annotations) / sizeof(lcr_annotation_item_t); + + // trans annotations + for (i = 0; i < anno->len; i++) { + if (anno->keys[i] == NULL) { + continue; + } + for (j = 0; j < len; j++) { + if (strcmp(anno->keys[i], g_require_annotations[j].name) != 0) { + continue; + } + + ret = g_require_annotations[j].checker(anno->values[i]); + if (ret == -1) { + ERROR("item: %s, value: %s, checker failed", anno->keys[i], anno->values[i]); + goto out_free; + } else if (ret == 1) { + DEBUG("Skip this config item: %s", anno->keys[i]); + continue; + } + + struct lcr_list *node = create_lcr_list_node(g_require_annotations[j].lxc_item_name, anno->values[i]); + if (node == NULL) { + goto out_free; + } + lcr_list_add_tail(conf, node); + break; + } + } + + return conf; +out_free: + lcr_free_config(conf); + free(conf); + + return NULL; +} + +static int add_needed_pty_conf(struct lcr_list *conf) +{ + struct lcr_list *node = create_lcr_list_node("lxc.pty.max", "1024"); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + + return 0; +} + +static int add_needed_tty_conf(struct lcr_list *conf) +{ + struct lcr_list *node = create_lcr_list_node("lxc.tty.max", "4"); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + + node = create_lcr_list_node("lxc.tty.dir", "lxc"); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + return 0; +} + +static int add_needed_net_conf(struct lcr_list *conf) +{ + struct lcr_list *node = create_lcr_list_node("lxc.net.0.type", "empty"); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + + node = create_lcr_list_node("lxc.net.0.flags", "up"); + if (node == NULL) { + return -1; + } + lcr_list_add_tail(conf, node); + return 0; +} + +/* get needed lxc conf */ +struct lcr_list *get_needed_lxc_conf() +{ + struct lcr_list *conf = util_common_calloc_s(sizeof(*conf)); + if (conf == NULL) { + return NULL; + } + lcr_list_init(conf); + + if (add_needed_tty_conf(conf) < 0) { + goto out_free; + } + if (add_needed_pty_conf(conf) < 0) { + goto out_free; + } + if (add_needed_net_conf(conf) < 0) { + goto out_free; + } + + return conf; +out_free: + lcr_free_config(conf); + free(conf); + return NULL; +} diff --git a/src/conf.h b/src/conf.h new file mode 100644 index 0000000..f7c3ffb --- /dev/null +++ b/src/conf.h @@ -0,0 +1,110 @@ +/****************************************************************************** + * 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 conf function + ******************************************************************************/ +#ifndef __LCR_CONF_H +#define __LCR_CONF_H + +#include "oci_runtime_spec.h" +#include "lcr_list.h" + +#define INVALID_INT 0 + +/* + * Store lcr configuration + */ +typedef struct { + char *name; + char *value; +} lcr_config_item_t; + +/* + * return value: + * 0 is valid item + * 1 will skip this + * -1 is invalid item + * */ +typedef int(*lcr_check_item_t)(const char *); +/* + * Store annotations configuration + * */ +typedef struct { + char *name; + char *lxc_item_name; + lcr_check_item_t checker; +} lcr_annotation_item_t; + +/* + * seccomp trans arch + * */ +typedef struct { + char *arch; + char *value; +} lcr_arch_value; + +/* + * get host arch + * */ +typedef struct { + char *arch; + char *value; + int num; +} lcr_host_arch; + +/* + * Create a lcr_list node, and initialize the elem to a lcr_config_item_t with + * key and value + */ +struct lcr_list *create_lcr_list_node(const char *key, const char *value); + +/* + * Free a lcr_list node + */ +void free_lcr_list_node(struct lcr_list *node); + +/* + * Translate oci hostname to lcr config + */ +struct lcr_list *trans_oci_hostname(const char *hostname); + +/* + * Translate oci process struct to lcr config + */ +struct lcr_list *trans_oci_process(const oci_runtime_spec_process *proc); + +/* + * Translate oci root struct to lcr config + */ +struct lcr_list *trans_oci_root(const oci_runtime_spec_root *root, const oci_runtime_config_linux *linux); +/* + * Translate oci mounts struct to lcr config + */ +struct lcr_list *trans_oci_mounts(const oci_runtime_spec *c); + +/* + * Translate oci linux struct to lcr config + */ +struct lcr_list *trans_oci_linux(const oci_runtime_config_linux *l, char **seccomp_conf); + +/* + * Translate oci annotations to lcr config + * This is not supported in standard oci runtime-spec + */ +struct lcr_list *trans_annotations(const json_map_string_string *anno); + +/* + * Get other lxc needed configurations + */ +struct lcr_list *get_needed_lxc_conf(); + +#endif /*__LCR_CONF_H*/ diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 0000000..bfa1212 --- /dev/null +++ b/src/constants.h @@ -0,0 +1,50 @@ +/****************************************************************************** + * 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 constant definition + ******************************************************************************/ + +#ifndef _LCRD_CONSTANTS_H +#define _LCRD_CONSTANTS_H + +/* mode of file and directory */ + +#define DEFAULT_SECURE_FILE_MODE 0640 + +#define DEFAULT_SECURE_DIRECTORY_MODE 0750 + +#define ROOTFS_MNT_DIRECTORY_MODE 0640 + +#define CONFIG_DIRECTORY_MODE 0750 + +#define CONFIG_FILE_MODE 0640 + +#define ARCH_LOG_FILE_MODE 0440 + +#define WORKING_LOG_FILE_MODE 0640 + +#define LOG_DIRECTORY_MODE 0750 + +#define TEMP_DIRECTORY_MODE 0750 + +#define DEBUG_FILE_MODE 0640 + +#define DEBUG_DIRECTORY_MODE 0750 + +// Config file path +#define OCIHOOKSFILE "ocihooks.json" +#define OCICONFIGFILE "ociconfig.json" +#define LXCCONFIGFILE "config" + +#define PARAM_NUM 50 + +#endif diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..a2619cf --- /dev/null +++ b/src/error.c @@ -0,0 +1,116 @@ +/****************************************************************************** + * 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 error definition + ******************************************************************************/ +#include "utils.h" +#include "securec.h" +#include "error.h" + +// record the lcr error +__thread engine_error_t g_lcr_error = { + .errcode = LCR_SUCCESS, + .errmsg = NULL +}; + +#define LCR_ERRMSG_GEN(n, s) { LCR_##n, s }, +struct lcr_strerror_tab_t { + lcr_errno_t lcr_errno; + const char *errmsg; +}; +static const struct lcr_strerror_tab_t g_lcr_strerror_tab[] = { + LCR_ERRNO_MAP(LCR_ERRMSG_GEN) +}; +#undef LCR_ERRMSG_GEN + +const char *errno_to_error_message(lcr_errno_t err) +{ + if ((size_t)err >= sizeof(g_lcr_strerror_tab) / sizeof(g_lcr_strerror_tab[0])) { + return g_lcr_strerror_tab[LCR_ERR_UNKNOWN].errmsg; + } + return g_lcr_strerror_tab[err].errmsg; +} + +void clear_error_message(engine_error_t *error) +{ + if (error == NULL) { + return; + } + error->errcode = LCR_SUCCESS; + free(error->errmsg); + error->errmsg = NULL; +} + +void lcr_set_error_message(lcr_errno_t errcode, const char *format, ...) +{ + int ret = 0; + char errbuf[BUFSIZ + 1] = { 0 }; + + va_list argp; + va_start(argp, format); + + ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + va_end(argp); + clear_error_message(&g_lcr_error); + if (ret < 0) { + g_lcr_error.errcode = LCR_ERR_FORMAT; + return; + } + g_lcr_error.errcode = errcode; + g_lcr_error.errmsg = util_strdup_s(errbuf); +} + +void lcr_try_set_error_message(lcr_errno_t errcode, const char *format, ...) +{ + int ret = 0; + char errbuf[BUFSIZ + 1] = { 0 }; + va_list argp; + + if (g_lcr_error.errmsg != NULL || g_lcr_error.errcode != LCR_SUCCESS) { + return; + } + va_start(argp, format); + ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + va_end(argp); + clear_error_message(&g_lcr_error); + if (ret < 0) { + g_lcr_error.errcode = LCR_ERR_FORMAT; + return; + } + g_lcr_error.errcode = errcode; + g_lcr_error.errmsg = util_strdup_s(errbuf); +} + +void lcr_append_error_message(lcr_errno_t errcode, const char *format, ...) +{ + int ret = 0; + char errbuf[BUFSIZ + 1] = { 0 }; + char *result = NULL; + + va_list argp; + va_start(argp, format); + + ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + va_end(argp); + if (ret < 0) { + g_lcr_error.errcode = LCR_ERR_FORMAT; + return; + } + g_lcr_error.errcode = errcode; + result = util_string_append(g_lcr_error.errmsg, errbuf); + if (result == NULL) { + g_lcr_error.errcode = LCR_ERR_MEMOUT; + return; + } + free(g_lcr_error.errmsg); + g_lcr_error.errmsg = result; +} diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..3fd60af --- /dev/null +++ b/src/error.h @@ -0,0 +1,68 @@ +/****************************************************************************** + * 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 error definition + ******************************************************************************/ +#ifndef __LCR_ERROR_H +#define __LCR_ERROR_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct engine_error { + uint32_t errcode; + char *errmsg; +} engine_error_t; + +/*record the lcr error*/ +extern __thread engine_error_t g_lcr_error; + +#define DEF_SUCCESS_STR "Success" +#define DEF_ERR_RUNTIME_STR "Runtime error" + +#define LCR_ERRNO_MAP(XX) \ + XX(SUCCESS, DEF_SUCCESS_STR) \ + \ + /* err in posix api call */ \ + XX(ERR_MEMOUT, "Out of memory") \ + XX(ERR_MEMSET, "Memory set error") \ + \ + /* err in other case or call function int thirdparty library */ \ + XX(ERR_FORMAT, "Error message is too long") \ + XX(ERR_INPUT, "Invalid input parameter") \ + XX(ERR_INTERNAL, "Server internal error") \ + \ + /* err in runtime module */ \ + XX(ERR_RUNTIME, DEF_ERR_RUNTIME_STR) \ + \ + /* err max */ \ + XX(ERR_UNKNOWN, "Unknown error") + +#define LCR_ERRNO_GEN(n, s) LCR_##n, +typedef enum { LCR_ERRNO_MAP(LCR_ERRNO_GEN) } lcr_errno_t; +#undef LCR_ERRNO_GEN + +const char *errno_to_error_message(lcr_errno_t err); + +void clear_error_message(engine_error_t *error); + +void lcr_set_error_message(lcr_errno_t errcode, const char *format, ...); + +void lcr_try_set_error_message(lcr_errno_t errcode, const char *format, ...); + +void lcr_append_error_message(lcr_errno_t errcode, const char *format, ...); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/json/CMakeLists.txt b/src/json/CMakeLists.txt new file mode 100644 index 0000000..d3cd170 --- /dev/null +++ b/src/json/CMakeLists.txt @@ -0,0 +1,2 @@ +# generate .c and .h files for json +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/schema) diff --git a/src/json/oci_runtime_hooks.c b/src/json/oci_runtime_hooks.c new file mode 100644 index 0000000..154a6e4 --- /dev/null +++ b/src/json/oci_runtime_hooks.c @@ -0,0 +1,82 @@ +/****************************************************************************** + * 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: maoweiyong + * Create: 2018-11-08 + * Description: provide oci runtime hooks functions + ******************************************************************************/ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include "read_file.h" +#include "oci_runtime_hooks.h" + +#define PARSE_ERR_BUFFER_SIZE 1024 + +char *oci_runtime_spec_hooks_generate_json(const oci_runtime_spec_hooks *ptr, const struct parser_context *ctx, + parser_error *err) +{ + yajl_gen g = NULL; + struct parser_context tmp_ctx = { 0 }; + const unsigned char *gen_buf = NULL; + char *json_buf = NULL; + size_t gen_len = 0; + errno_t eret; + + if (ptr == NULL || err == NULL) { + return NULL; + } + + *err = NULL; + if (ctx == NULL) { + ctx = &tmp_ctx; + } + + if (!json_gen_init(&g, ctx)) { + *err = strdup("Json_gen init failed"); + goto out; + } + if (yajl_gen_status_ok != gen_oci_runtime_spec_hooks(g, ptr, ctx, err)) { + if (*err == NULL) { + *err = strdup("Failed to generate json"); + } + goto free_out; + } + yajl_gen_get_buf(g, &gen_buf, &gen_len); + if (gen_buf == NULL) { + *err = strdup("Error to get generated json"); + goto free_out; + } + + if (gen_len > SIZE_MAX - 1) { + *err = strdup("Generated json too long"); + goto free_out; + } + + json_buf = malloc(gen_len + 1); + if (json_buf == NULL) { + *err = strdup("Out of memory"); + goto free_out; + } + eret = memcpy_s((void *)json_buf, gen_len + 1, (void *)gen_buf, gen_len); + if (eret != EOK) { + *err = strdup("Memcpy failed"); + goto free_out; + } + json_buf[gen_len] = '\0'; + +free_out: + yajl_gen_clear(g); + yajl_gen_free(g); +out: + return json_buf; +} diff --git a/src/json/oci_runtime_hooks.h b/src/json/oci_runtime_hooks.h new file mode 100644 index 0000000..2e7bb05 --- /dev/null +++ b/src/json/oci_runtime_hooks.h @@ -0,0 +1,24 @@ +/****************************************************************************** + * 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: maoweiyong + * Create: 2018-11-08 + * Description: provide oci runtime hooks functions + ******************************************************************************/ + +#ifndef _CONTAINER_HOOKS_H +#define _CONTAINER_HOOKS_H + +#include "oci_runtime_spec.h" + +char *oci_runtime_spec_hooks_generate_json(const oci_runtime_spec_hooks *ptr, const struct parser_context *ctx, + parser_error *err); + +#endif diff --git a/src/json/schema/CMakeLists.txt b/src/json/schema/CMakeLists.txt new file mode 100644 index 0000000..80d6703 --- /dev/null +++ b/src/json/schema/CMakeLists.txt @@ -0,0 +1,16 @@ +project (JSONGenerator) + +set(cmdpath python) +set(pysrcpath ${CMAKE_CURRENT_SOURCE_DIR}/src/generate.py) +set(schemapath ${CMAKE_CURRENT_SOURCE_DIR}/schema) +set(outputpath ${CMAKE_BINARY_DIR}/json) +add_subdirectory(src) + +message("-- Generate .c and .h file into: " ${outputpath}) + +execute_process(COMMAND ${cmdpath} ${pysrcpath} --gen-common --gen-ref -r --root=${schemapath} --out=${outputpath} ${schemapath} + ERROR_VARIABLE err + ) +if (err) + message("error: " ${err}) +endif() diff --git a/src/json/schema/schema/defs.json b/src/json/schema/schema/defs.json new file mode 100644 index 0000000..c8739d1 --- /dev/null +++ b/src/json/schema/schema/defs.json @@ -0,0 +1,233 @@ +{ + " description": "Definitions used throughout the OpenContainer Specification", + "definitions": { + "int8": { + "type": "integer", + "minimum": -128, + "maximum": 127 + }, + "int16": { + "type": "integer", + "minimum": -32768, + "maximum": 32767 + }, + "int32": { + "type": "integer", + "minimum": -2147483648, + "maximum": 2147483647 + }, + "int64": { + "type": "integer", + "minimum": -9223372036854776000, + "maximum": 9223372036854776000 + }, + "uint8": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "uint16": { + "type": "integer", + "minimum": 0, + "maximum": 65535 + }, + "uint32": { + "type": "integer", + "minimum": 0, + "maximum": 4294967295 + }, + "uint64": { + "type": "integer", + "minimum": 0, + "maximum": 18446744073709552000 + }, + "uint16Pointer": { + "oneOf": [ + { + "$ref": "#/definitions/uint16" + }, + { + "type": "null" + } + ] + }, + "uint64Pointer": { + "oneOf": [ + { + "$ref": "#/definitions/uint64" + }, + { + "type": "null" + } + ] + }, + "stringPointer": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "percent": { + "type": "integer", + "minimum": 0, + "maximum": 100 + }, + "UID": { + "$ref": "#/definitions/uint32" + }, + "GID": { + "$ref": "#/definitions/uint32" + }, + "ArrayOfGIDs": { + "type": "array", + "items": { + "$ref": "#/definitions/GID" + } + }, + "FilePath": { + "type": "string" + }, + "Env": { + "$ref": "#/definitions/ArrayOfStrings" + }, + "Hook": { + "type": "object", + "properties": { + "path": { + "$ref": "#/definitions/FilePath" + }, + "args": { + "$ref": "#/definitions/ArrayOfStrings" + }, + "env": { + "$ref": "#/definitions/Env" + }, + "timeout": { + "type": "integer", + "minimum": 1 + } + }, + "required": [ + "path" + ] + }, + "ArrayOfHooks": { + "type": "array", + "items": { + "$ref": "#/definitions/Hook" + } + }, + "IDMapping": { + "type": "object", + "properties": { + "hostID": { + "$ref": "#/definitions/uint32" + }, + "containerID": { + "$ref": "#/definitions/uint32" + }, + "size": { + "$ref": "#/definitions/uint32" + } + }, + "required": [ + "hostID", + "containerID", + "size" + ] + }, + "Mount": { + "type": "object", + "properties": { + "source": { + "$ref": "#/definitions/FilePath" + }, + "destination": { + "$ref": "#/definitions/FilePath" + }, + "options": { + "$ref": "#/definitions/ArrayOfStrings" + }, + "type": { + "type": "string" + } + }, + "required": [ + "destination" + ] + }, + "ArrayOfStrings": { + "type": "array", + "items": { + "type": "string" + } + }, + "mapStringString": { + "type": "object", + "patternProperties": { + ".{1,}": { + "type": "string" + } + } + }, + "mapStringInt": { + "type": "object", + "patternProperties": { + ".{1,}": { + "type": "integer" + } + } + }, + "mapStringBool": { + "type": "object", + "patternProperties": { + ".{1,}": { + "type": "boolean" + } + } + }, + "mapIntString": { + "type": "object", + "patternProperties": { + ".{2,}": { + "type": "string" + } + } + }, + "mapIntInt": { + "type": "object", + "patternProperties": { + ".{2,}": { + "type": "integer" + } + } + }, + "mapIntBool": { + "type": "object", + "patternProperties": { + ".{2,}": { + "type": "boolean" + } + } + }, + "mapStringObject": { + "type": "object", + "patternProperties": { + ".{1,}": { + "type": "object" + } + } + }, + "ociVersion": { + "description": "The version of Open Container Runtime Specification that the document complies with", + "type": "string" + }, + "annotations": { + "$ref": "#/definitions/mapStringString" + } + } +} diff --git a/src/json/schema/schema/oci/runtime/config-linux.json b/src/json/schema/schema/oci/runtime/config-linux.json new file mode 100644 index 0000000..95935f5 --- /dev/null +++ b/src/json/schema/schema/oci/runtime/config-linux.json @@ -0,0 +1,271 @@ +{ + "linux": { + "description": "Linux platform-specific configurations", + "id": "https://opencontainers.org/schema/bundle/linux", + "type": "object", + "properties": { + "devices": { + "id": "https://opencontainers.org/schema/bundle/linux/devices", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/Device" + } + }, + "uidMappings": { + "id": "https://opencontainers.org/schema/bundle/linux/uidMappings", + "type": "array", + "items": { + "$ref": "defs.json#/definitions/IDMapping" + } + }, + "gidMappings": { + "id": "https://opencontainers.org/schema/bundle/linux/gidMappings", + "type": "array", + "items": { + "$ref": "defs.json#/definitions/IDMapping" + } + }, + "namespaces": { + "id": "https://opencontainers.org/schema/bundle/linux/namespaces", + "type": "array", + "items": { + "anyOf": [ + { + "$ref": "defs-linux.json#/definitions/NamespaceReference" + } + ] + } + }, + "resources": { + "id": "https://opencontainers.org/schema/bundle/linux/resources", + "type": "object", + "properties": { + "devices": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/devices", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/DeviceCgroup" + } + }, + "pids": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/pids", + "type": "object", + "properties": { + "limit": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/pids/limit", + "$ref": "defs.json#/definitions/int64" + } + }, + "required": [ + "limit" + ] + }, + "blockIO": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/blockIO", + "type": "object", + "properties": { + "weight": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/blockIO/weight", + "$ref": "defs-linux.json#/definitions/weight" + }, + "leafWeight": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/blockIO/leafWeight", + "$ref": "defs-linux.json#/definitions/weight" + }, + "throttleReadBpsDevice": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/blockIO/throttleReadBpsDevice", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/blockIODeviceThrottle" + } + }, + "throttleWriteBpsDevice": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/blockIO/throttleWriteBpsDevice", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/blockIODeviceThrottle" + } + }, + "throttleReadIOPSDevice": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/blockIO/throttleReadIOPSDevice", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/blockIODeviceThrottle" + } + }, + "throttleWriteIOPSDevice": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/blockIO/throttleWriteIOPSDevice", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/blockIODeviceThrottle" + } + }, + "weightDevice": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/blockIO/weightDevice", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/blockIODeviceWeight" + } + } + } + }, + "cpu": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu", + "type": "object", + "properties": { + "cpus": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/cpus", + "type": "string" + }, + "mems": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/mems", + "type": "string" + }, + "period": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/period", + "$ref": "defs.json#/definitions/uint64" + }, + "quota": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/quota", + "$ref": "defs.json#/definitions/int64" + }, + "realtimePeriod": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/realtimePeriod", + "$ref": "defs.json#/definitions/uint64" + }, + "realtimeRuntime": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/realtimeRuntime", + "$ref": "defs.json#/definitions/int64" + }, + "shares": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/shares", + "$ref": "defs.json#/definitions/uint64" + } + } + }, + "hugepageLimits": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/hugepageLimits", + "type": "array", + "items": { + "type": "object", + "properties": { + "pageSize": { + "type": "string" + }, + "limit": { + "$ref": "defs.json#/definitions/uint64" + } + }, + "required": [ + "pageSize", + "limit" + ] + } + }, + "memory": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/memory", + "type": "object", + "properties": { + "kernel": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/kernel", + "$ref": "defs.json#/definitions/int64" + }, + "kernelTCP": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/kernelTCP", + "$ref": "defs.json#/definitions/int64" + }, + "limit": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/limit", + "$ref": "defs.json#/definitions/int64" + }, + "reservation": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/reservation", + "$ref": "defs.json#/definitions/int64" + }, + "swap": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/swap", + "$ref": "defs.json#/definitions/int64" + }, + "swappiness": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/swappiness", + "$ref": "defs.json#/definitions/uint64" + }, + "disableOOMKiller": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/disableOOMKiller", + "type": "boolean" + } + } + }, + "network": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/network", + "type": "object", + "properties": { + "classID": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/network/classId", + "$ref": "defs.json#/definitions/uint32" + }, + "priorities": { + "id": "https://opencontainers.org/schema/bundle/linux/resources/network/priorities", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/NetworkInterfacePriority" + } + } + } + } + } + }, + "cgroupsPath": { + "id": "https://opencontainers.org/schema/bundle/linux/cgroupsPath", + "type": "string" + }, + "rootfsPropagation": { + "id": "https://opencontainers.org/schema/bundle/linux/rootfsPropagation", + "$ref": "defs-linux.json#/definitions/RootfsPropagation" + }, + "seccomp": { + "id": "https://opencontainers.org/schema/bundle/linux/seccomp", + "type": "object", + "properties": { + "defaultAction": { + "id": "https://opencontainers.org/schema/bundle/linux/seccomp/defaultAction", + "type": "string" + }, + "architectures": { + "id": "https://opencontainers.org/schema/bundle/linux/seccomp/architectures", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/SeccompArch" + } + }, + "syscalls": { + "id": "https://opencontainers.org/schema/bundle/linux/seccomp/syscalls", + "type": "array", + "items": { + "$ref": "defs-linux.json#/definitions/Syscall" + } + } + }, + "required": [ + "defaultAction" + ] + }, + "sysctl": { + "id": "https://opencontainers.org/schema/bundle/linux/sysctl", + "$ref": "defs.json#/definitions/mapStringString" + }, + "maskedPaths": { + "id": "https://opencontainers.org/schema/bundle/linux/maskedPaths", + "$ref": "defs.json#/definitions/ArrayOfStrings" + }, + "readonlyPaths": { + "id": "https://opencontainers.org/schema/bundle/linux/readonlyPaths", + "$ref": "defs.json#/definitions/ArrayOfStrings" + }, + "mountLabel": { + "id": "https://opencontainers.org/schema/bundle/linux/mountLabel", + "type": "string" + } + } + } +} diff --git a/src/json/schema/schema/oci/runtime/defs-linux.json b/src/json/schema/schema/oci/runtime/defs-linux.json new file mode 100644 index 0000000..4d9620a --- /dev/null +++ b/src/json/schema/schema/oci/runtime/defs-linux.json @@ -0,0 +1,270 @@ +{ + "definitions": { + "RootfsPropagation": { + "type": "string", + "enum": [ + "private", + "shared", + "slave", + "unbindable" + ] + }, + "SeccompArch": { + "type": "string", + "enum": [ + "SCMP_ARCH_X86", + "SCMP_ARCH_X86_64", + "SCMP_ARCH_X32", + "SCMP_ARCH_ARM", + "SCMP_ARCH_AARCH64", + "SCMP_ARCH_MIPS", + "SCMP_ARCH_MIPS64", + "SCMP_ARCH_MIPS64N32", + "SCMP_ARCH_MIPSEL", + "SCMP_ARCH_MIPSEL64", + "SCMP_ARCH_MIPSEL64N32", + "SCMP_ARCH_PPC", + "SCMP_ARCH_PPC64", + "SCMP_ARCH_PPC64LE", + "SCMP_ARCH_S390", + "SCMP_ARCH_S390X", + "SCMP_ARCH_PARISC", + "SCMP_ARCH_PARISC64" + ] + }, + "SeccompAction": { + "type": "string", + "enum": [ + "SCMP_ACT_KILL", + "SCMP_ACT_TRAP", + "SCMP_ACT_ERRNO", + "SCMP_ACT_TRACE", + "SCMP_ACT_ALLOW" + ] + }, + "SeccompOperators": { + "type": "string", + "enum": [ + "SCMP_CMP_NE", + "SCMP_CMP_LT", + "SCMP_CMP_LE", + "SCMP_CMP_EQ", + "SCMP_CMP_GE", + "SCMP_CMP_GT", + "SCMP_CMP_MASKED_EQ" + ] + }, + "SyscallArg": { + "type": "object", + "properties": { + "index": { + "$ref": "defs.json#/definitions/uint32" + }, + "value": { + "$ref": "defs.json#/definitions/uint64" + }, + "valueTwo": { + "$ref": "defs.json#/definitions/uint64" + }, + "op": { + "$ref": "#/definitions/SeccompOperators" + } + }, + "required": [ + "index", + "value", + "op" + ] + }, + "Syscall": { + "type": "object", + "properties": { + "names": { + "type": "array", + "items": { + "type": "string" + }, + "minItems": 1 + }, + "action": { + "$ref": "#/definitions/SeccompAction" + }, + "args": { + "type": "array", + "items": { + "$ref": "#/definitions/SyscallArg" + } + } + }, + "required": [ + "names", + "action" + ] + }, + "Major": { + "description": "major device number", + "$ref": "defs.json#/definitions/int64" + }, + "Minor": { + "description": "minor device number", + "$ref": "defs.json#/definitions/int64" + }, + "FileMode": { + "description": "File permissions mode (typically an octal value)", + "type": "integer", + "minimum": 0, + "maximum": 512 + }, + "FileType": { + "description": "Type of a block or special character device", + "type": "string", + "pattern": "^[cbup]$" + }, + "Device": { + "type": "object", + "required": [ + "type", + "path" + ], + "properties": { + "type": { + "$ref": "#/definitions/FileType" + }, + "path": { + "$ref": "defs.json#/definitions/FilePath" + }, + "fileMode": { + "$ref": "#/definitions/FileMode" + }, + "major": { + "$ref": "#/definitions/Major" + }, + "minor": { + "$ref": "#/definitions/Minor" + }, + "uid": { + "$ref": "defs.json#/definitions/UID" + }, + "gid": { + "$ref": "defs.json#/definitions/GID" + } + } + }, + "weight": { + "type": "integer" + }, + "blockIODevice": { + "type": "object", + "properties": { + "major": { + "$ref": "#/definitions/Major" + }, + "minor": { + "$ref": "#/definitions/Minor" + } + }, + "required": [ + "major", + "minor" + ] + }, + "blockIODeviceWeight": { + "type": "object", + "allOf": [ + { + "$ref": "#/definitions/blockIODevice" + }, + { + "type": "object", + "properties": { + "weight": { + "$ref": "#/definitions/weight" + }, + "leafWeight": { + "$ref": "#/definitions/weight" + } + } + } + ] + }, + "blockIODeviceThrottle": { + "allOf": [ + { + "$ref": "#/definitions/blockIODevice" + }, + { + "type": "object", + "properties": { + "rate": { + "$ref": "defs.json#/definitions/uint64" + } + } + } + ] + }, + "DeviceCgroup": { + "type": "object", + "properties": { + "allow": { + "type": "boolean" + }, + "type": { + "type": "string" + }, + "major": { + "$ref": "#/definitions/Major" + }, + "minor": { + "$ref": "#/definitions/Minor" + }, + "access": { + "type": "string" + } + }, + "required": [ + "allow" + ] + }, + "NetworkInterfacePriority": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "priority": { + "$ref": "defs.json#/definitions/uint32" + } + }, + "required": [ + "name", + "priority" + ] + }, + "NamespaceType": { + "type": "string", + "enum": [ + "mount", + "pid", + "network", + "uts", + "ipc", + "user", + "cgroup" + ] + }, + "NamespaceReference": { + "type": "object", + "properties": { + "type": { + "$ref": "#/definitions/NamespaceType" + }, + "path": { + "$ref": "defs.json#/definitions/FilePath" + } + }, + "required": [ + "type" + ] + } + } +} diff --git a/src/json/schema/schema/oci/runtime/defs.json b/src/json/schema/schema/oci/runtime/defs.json new file mode 100644 index 0000000..c8739d1 --- /dev/null +++ b/src/json/schema/schema/oci/runtime/defs.json @@ -0,0 +1,233 @@ +{ + " description": "Definitions used throughout the OpenContainer Specification", + "definitions": { + "int8": { + "type": "integer", + "minimum": -128, + "maximum": 127 + }, + "int16": { + "type": "integer", + "minimum": -32768, + "maximum": 32767 + }, + "int32": { + "type": "integer", + "minimum": -2147483648, + "maximum": 2147483647 + }, + "int64": { + "type": "integer", + "minimum": -9223372036854776000, + "maximum": 9223372036854776000 + }, + "uint8": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "uint16": { + "type": "integer", + "minimum": 0, + "maximum": 65535 + }, + "uint32": { + "type": "integer", + "minimum": 0, + "maximum": 4294967295 + }, + "uint64": { + "type": "integer", + "minimum": 0, + "maximum": 18446744073709552000 + }, + "uint16Pointer": { + "oneOf": [ + { + "$ref": "#/definitions/uint16" + }, + { + "type": "null" + } + ] + }, + "uint64Pointer": { + "oneOf": [ + { + "$ref": "#/definitions/uint64" + }, + { + "type": "null" + } + ] + }, + "stringPointer": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "percent": { + "type": "integer", + "minimum": 0, + "maximum": 100 + }, + "UID": { + "$ref": "#/definitions/uint32" + }, + "GID": { + "$ref": "#/definitions/uint32" + }, + "ArrayOfGIDs": { + "type": "array", + "items": { + "$ref": "#/definitions/GID" + } + }, + "FilePath": { + "type": "string" + }, + "Env": { + "$ref": "#/definitions/ArrayOfStrings" + }, + "Hook": { + "type": "object", + "properties": { + "path": { + "$ref": "#/definitions/FilePath" + }, + "args": { + "$ref": "#/definitions/ArrayOfStrings" + }, + "env": { + "$ref": "#/definitions/Env" + }, + "timeout": { + "type": "integer", + "minimum": 1 + } + }, + "required": [ + "path" + ] + }, + "ArrayOfHooks": { + "type": "array", + "items": { + "$ref": "#/definitions/Hook" + } + }, + "IDMapping": { + "type": "object", + "properties": { + "hostID": { + "$ref": "#/definitions/uint32" + }, + "containerID": { + "$ref": "#/definitions/uint32" + }, + "size": { + "$ref": "#/definitions/uint32" + } + }, + "required": [ + "hostID", + "containerID", + "size" + ] + }, + "Mount": { + "type": "object", + "properties": { + "source": { + "$ref": "#/definitions/FilePath" + }, + "destination": { + "$ref": "#/definitions/FilePath" + }, + "options": { + "$ref": "#/definitions/ArrayOfStrings" + }, + "type": { + "type": "string" + } + }, + "required": [ + "destination" + ] + }, + "ArrayOfStrings": { + "type": "array", + "items": { + "type": "string" + } + }, + "mapStringString": { + "type": "object", + "patternProperties": { + ".{1,}": { + "type": "string" + } + } + }, + "mapStringInt": { + "type": "object", + "patternProperties": { + ".{1,}": { + "type": "integer" + } + } + }, + "mapStringBool": { + "type": "object", + "patternProperties": { + ".{1,}": { + "type": "boolean" + } + } + }, + "mapIntString": { + "type": "object", + "patternProperties": { + ".{2,}": { + "type": "string" + } + } + }, + "mapIntInt": { + "type": "object", + "patternProperties": { + ".{2,}": { + "type": "integer" + } + } + }, + "mapIntBool": { + "type": "object", + "patternProperties": { + ".{2,}": { + "type": "boolean" + } + } + }, + "mapStringObject": { + "type": "object", + "patternProperties": { + ".{1,}": { + "type": "object" + } + } + }, + "ociVersion": { + "description": "The version of Open Container Runtime Specification that the document complies with", + "type": "string" + }, + "annotations": { + "$ref": "#/definitions/mapStringString" + } + } +} diff --git a/src/json/schema/schema/oci/runtime/spec.json b/src/json/schema/schema/oci/runtime/spec.json new file mode 100644 index 0000000..d083274 --- /dev/null +++ b/src/json/schema/schema/oci/runtime/spec.json @@ -0,0 +1,216 @@ +{ + "description": "Open Container Runtime Specification Container Configuration Schema", + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "https://opencontainers.org/schema/bundle", + "type": "object", + "properties": { + "ociVersion": { + "id": "https://opencontainers.org/schema/bundle/ociVersion", + "$ref": "defs.json#/definitions/ociVersion" + }, + "hooks": { + "id": "https://opencontainers.org/schema/bundle/hooks", + "type": "object", + "properties": { + "prestart": { + "$ref": "defs.json#/definitions/ArrayOfHooks" + }, + "poststart": { + "$ref": "defs.json#/definitions/ArrayOfHooks" + }, + "poststop": { + "$ref": "defs.json#/definitions/ArrayOfHooks" + } + } + }, + "annotations": { + "$ref": "defs.json#/definitions/annotations" + }, + "hostname": { + "id": "https://opencontainers.org/schema/bundle/hostname", + "type": "string" + }, + "mounts": { + "id": "https://opencontainers.org/schema/bundle/mounts", + "type": "array", + "items": { + "$ref": "defs.json#/definitions/Mount" + } + }, + "root": { + "description": "Configures the container's root filesystem.", + "id": "https://opencontainers.org/schema/bundle/root", + "type": "object", + "required": [ + "path" + ], + "properties": { + "path": { + "id": "https://opencontainers.org/schema/bundle/root/path", + "$ref": "defs.json#/definitions/FilePath" + }, + "readonly": { + "id": "https://opencontainers.org/schema/bundle/root/readonly", + "type": "boolean" + } + } + }, + "process": { + "id": "https://opencontainers.org/schema/bundle/process", + "type": "object", + "required": [ + "cwd", + "args" + ], + "properties": { + "args": { + "id": "https://opencontainers.org/schema/bundle/process/args", + "$ref": "defs.json#/definitions/ArrayOfStrings" + }, + "consoleSize": { + "id": "https://opencontainers.org/schema/bundle/process/consoleSize", + "type": "object", + "required": [ + "height", + "width" + ], + "properties": { + "height": { + "id": "https://opencontainers.org/schema/bundle/process/consoleSize/height", + "$ref": "defs.json#/definitions/uint64" + }, + "width": { + "id": "https://opencontainers.org/schema/bundle/process/consoleSize/width", + "$ref": "defs.json#/definitions/uint64" + } + } + }, + "cwd": { + "id": "https://opencontainers.org/schema/bundle/process/cwd", + "type": "string" + }, + "env": { + "id": "https://opencontainers.org/schema/bundle/process/env", + "$ref": "defs.json#/definitions/Env" + }, + "terminal": { + "id": "https://opencontainers.org/schema/bundle/process/terminal", + "type": "boolean" + }, + "user": { + "id": "https://opencontainers.org/schema/bundle/process/user", + "type": "object", + "properties": { + "uid": { + "id": "https://opencontainers.org/schema/bundle/process/user/uid", + "$ref": "defs.json#/definitions/UID" + }, + "gid": { + "id": "https://opencontainers.org/schema/bundle/process/user/gid", + "$ref": "defs.json#/definitions/GID" + }, + "additionalGids": { + "id": "https://opencontainers.org/schema/bundle/process/user/additionalGids", + "$ref": "defs.json#/definitions/ArrayOfGIDs" + }, + "username": { + "id": "https://opencontainers.org/schema/bundle/process/user/username", + "type": "string" + } + } + }, + "capabilities": { + "id": "https://opencontainers.org/schema/bundle/process/linux/capabilities", + "type": "object", + "properties": { + "bounding": { + "id": "https://opencontainers.org/schema/bundle/process/linux/capabilities/bounding", + "type": "array", + "items": { + "type": "string" + } + }, + "permitted": { + "id": "https://opencontainers.org/schema/bundle/process/linux/capabilities/permitted", + "type": "array", + "items": { + "type": "string" + } + }, + "effective": { + "id": "https://opencontainers.org/schema/bundle/process/linux/capabilities/effective", + "type": "array", + "items": { + "type": "string" + } + }, + "inheritable": { + "id": "https://opencontainers.org/schema/bundle/process/linux/capabilities/inheritable", + "type": "array", + "items": { + "type": "string" + } + }, + "ambient": { + "id": "https://opencontainers.org/schema/bundle/process/linux/capabilities/ambient", + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "apparmorProfile": { + "id": "https://opencontainers.org/schema/bundle/process/linux/apparmorProfile", + "type": "string" + }, + "oomScoreAdj": { + "id": "https://opencontainers.org/schema/bundle/process/linux/oomScoreAdj", + "type": "integer" + }, + "selinuxLabel": { + "id": "https://opencontainers.org/schema/bundle/process/linux/selinuxLabel", + "type": "string" + }, + "noNewPrivileges": { + "id": "https://opencontainers.org/schema/bundle/process/linux/noNewPrivileges", + "type": "boolean" + }, + "rlimits": { + "id": "https://opencontainers.org/schema/bundle/linux/rlimits", + "type": "array", + "items": { + "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0", + "type": "object", + "required": [ + "type", + "soft", + "hard" + ], + "properties": { + "hard": { + "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/hard", + "$ref": "defs.json#/definitions/uint64" + }, + "soft": { + "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/soft", + "$ref": "defs.json#/definitions/uint64" + }, + "type": { + "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/type", + "type": "string", + "pattern": "^RLIMIT_[A-Z]+$" + } + } + } + } + } + }, + "linux": { + "$ref": "config-linux.json#/linux" + } + }, + "required": [ + "ociVersion" + ] +} diff --git a/src/json/schema/schema/oci/runtime/state.json b/src/json/schema/schema/oci/runtime/state.json new file mode 100644 index 0000000..563a619 --- /dev/null +++ b/src/json/schema/schema/oci/runtime/state.json @@ -0,0 +1,45 @@ +{ + "description": "Open Container Runtime State Schema", + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "https://opencontainers.org/schema/state", + "type": "object", + "properties": { + "ociVersion": { + "id": "https://opencontainers.org/schema/runtime/state/ociVersion", + "$ref": "defs.json#/definitions/ociVersion" + }, + "id": { + "id": "https://opencontainers.org/schema/runtime/state/id", + "description": "the container's ID", + "type": "string" + }, + "status": { + "id": "https://opencontainers.org/schema/runtime/state/status", + "type": "string", + "enum": [ + "creating", + "created", + "running", + "stopped" + ] + }, + "pid": { + "id": "https://opencontainers.org/schema/runtime/state/pid", + "type": "integer", + "minimum": 0 + }, + "bundle": { + "id": "https://opencontainers.org/schema/runtime/state/bundle", + "type": "string" + }, + "annotations": { + "$ref": "defs.json#/definitions/annotations" + } + }, + "required": [ + "ociVersion", + "id", + "status", + "bundle" + ] +} diff --git a/src/json/schema/schema/start-generate-config.json b/src/json/schema/schema/start-generate-config.json new file mode 100644 index 0000000..a7c470c --- /dev/null +++ b/src/json/schema/schema/start-generate-config.json @@ -0,0 +1,18 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "uid": { + "id": "https://opencontainers.org/schema/bundle/process/user/uid", + "$ref": "defs.json#/definitions/UID" + }, + "gid": { + "id": "https://opencontainers.org/schema/bundle/process/user/gid", + "$ref": "defs.json#/definitions/GID" + }, + "additionalGids": { + "id": "https://opencontainers.org/schema/bundle/process/user/additionalGids", + "$ref": "defs.json#/definitions/ArrayOfGIDs" + } + } +} diff --git a/src/json/schema/src/CMakeLists.txt b/src/json/schema/src/CMakeLists.txt new file mode 100644 index 0000000..8db178e --- /dev/null +++ b/src/json/schema/src/CMakeLists.txt @@ -0,0 +1,3 @@ +# get current directory sources files + +message("-- do nothing ") diff --git a/src/json/schema/src/common_c.py b/src/json/schema/src/common_c.py new file mode 100644 index 0000000..78a90f2 --- /dev/null +++ b/src/json/schema/src/common_c.py @@ -0,0 +1,1223 @@ +# -*- coding: utf-8 -*- +''' +Description: commom source file +Interface: None +History: 2019-06-17 +''' +# - Copyright (C) Huawei Technologies., 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. +# - Description: generate json +# - Author: tanyifeng +# - Create: 2018-04-25 +#!/usr/bin/python -Es + +CODE = '''// Auto generated file. Do not edit! +# define _GNU_SOURCE +# include +# include +# include +# include "json_common.h" + +# define MAX_NUM_STR_LEN 21 + + + +yajl_gen_status map_uint(void *ctx, long long unsigned int num) { + char numstr[MAX_NUM_STR_LEN]; + int ret; + + ret = sprintf_s(numstr, sizeof(numstr), "%llu", num); + if (ret < 0) { + return yajl_gen_in_error_state; + } + return yajl_gen_number((yajl_gen)ctx, (const char *)numstr, strlen(numstr)); +} + +yajl_gen_status map_int(void *ctx, long long int num) { + char numstr[MAX_NUM_STR_LEN]; + int ret; + + ret = sprintf_s(numstr, sizeof(numstr), "%lld", num); + if (ret < 0) { + return yajl_gen_in_error_state; + } + return yajl_gen_number((yajl_gen)ctx, (const char *)numstr, strlen(numstr)); +} + + +bool json_gen_init(yajl_gen *g, const struct parser_context *ctx) { + *g = yajl_gen_alloc(NULL); + if (NULL == *g) { + return false; + + } + yajl_gen_config(*g, yajl_gen_beautify, (int)(!(ctx->options & OPT_GEN_SIMPLIFY))); + yajl_gen_config(*g, yajl_gen_validate_utf8, (int)(!(ctx->options & OPT_GEN_NO_VALIDATE_UTF8))); + return true; +} + +yajl_val get_val(yajl_val tree, const char *name, yajl_type type) { + const char *path[] = { name, NULL }; + return yajl_tree_get(tree, path, type); +} + +void *safe_malloc(size_t size) { + void *ret = NULL; + if (size == 0) { + abort(); + } + ret = calloc(1, size); + if (ret == NULL) { + abort(); + } + return ret; +} + +int common_safe_double(const char *numstr, double *converted) { + char *err_str = NULL; + double d; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + d = strtod(numstr, &err_str); + if (errno > 0) { + return -errno; + } + + if (err_str == NULL || err_str == numstr || *err_str != '\\0') { + return -EINVAL; + } + + *converted = d; + return 0; +} + +int common_safe_uint8(const char *numstr, uint8_t *converted) { + char *err = NULL; + unsigned long int uli; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + uli = strtoul(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + if (uli > UINT8_MAX) { + return -ERANGE; + } + + *converted = (uint8_t)uli; + return 0; +} + +int common_safe_uint16(const char *numstr, uint16_t *converted) { + char *err = NULL; + unsigned long int uli; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + uli = strtoul(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + if (uli > UINT16_MAX) { + return -ERANGE; + } + + *converted = (uint16_t)uli; + return 0; +} + +int common_safe_uint32(const char *numstr, uint32_t *converted) { + char *err = NULL; + unsigned long long int ull; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + ull = strtoull(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + if (ull > UINT32_MAX) { + return -ERANGE; + } + + *converted = (uint32_t)ull; + return 0; +} + +int common_safe_uint64(const char *numstr, uint64_t *converted) { + char *err = NULL; + unsigned long long int ull; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + ull = strtoull(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + *converted = (uint64_t)ull; + return 0; +} + +int common_safe_uint(const char *numstr, unsigned int *converted) { + char *err = NULL; + unsigned long long int ull; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + ull = strtoull(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + if (ull > UINT_MAX) { + return -ERANGE; + } + + *converted = (unsigned int)ull; + return 0; +} + +int common_safe_int8(const char *numstr, int8_t *converted) { + char *err = NULL; + long int li; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + li = strtol(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + if (li > INT8_MAX || li < INT8_MIN) { + return -ERANGE; + } + + *converted = (int8_t)li; + return 0; +} + +int common_safe_int16(const char *numstr, int16_t *converted) { + char *err = NULL; + long int li; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + li = strtol(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + if (li > INT16_MAX || li < INT16_MIN) { + return -ERANGE; + } + + *converted = (int16_t)li; + return 0; +} + +int common_safe_int32(const char *numstr, int32_t *converted) { + char *err = NULL; + long long int lli; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + lli = strtol(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + if (lli > INT32_MAX || lli < INT32_MIN) { + return -ERANGE; + } + + *converted = (int32_t)lli; + return 0; +} + +int common_safe_int64(const char *numstr, int64_t *converted) { + char *err = NULL; + long long int lli; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + lli = strtoll(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + *converted = (int64_t)lli; + return 0; +} + +int common_safe_int(const char *numstr, int *converted) { + char *err = NULL; + long long int lli; + + if (numstr == NULL) { + return -EINVAL; + } + + errno = 0; + lli = strtol(numstr, &err, 0); + if (errno > 0) { + return -errno; + } + + if (err == NULL || err == numstr || *err != '\\0') { + return -EINVAL; + } + + if (lli > INT_MAX || lli < INT_MIN) { + return -ERANGE; + } + + *converted = (int)lli; + return 0; +} + +char *safe_strdup(const char *src) +{ + char *dst = NULL; + + if (src == NULL) { + return NULL; + } + + dst = strdup(src); + if (dst == NULL) { + abort(); + } + + return dst; +} + + +yajl_gen_status gen_json_map_int_int(void *ctx, const json_map_int_int *map, const struct parser_context *ptx, parser_error *err) { + yajl_gen_status stat = yajl_gen_status_ok; + yajl_gen g = (yajl_gen) ctx; + size_t len = 0, i = 0; + if (map != NULL) { + len = map->len; + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 0); + } + stat = yajl_gen_map_open((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + + } + for (i = 0; i < len; i++) { + char numstr[MAX_NUM_STR_LEN]; + int nret; + nret = sprintf_s(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); + if (nret < 0) { + if (!*err && asprintf(err, "Error to print string") < 0) { + *(err) = safe_strdup("error allocating memory"); + } + return yajl_gen_in_error_state; + } + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)numstr, strlen(numstr)); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + stat = map_int(g, map->values[i]); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + } + + stat = yajl_gen_map_close((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 1); + } + return yajl_gen_status_ok; +} + +void free_json_map_int_int(json_map_int_int *map) { + if (map != NULL) { + free(map->keys); + map->keys = NULL; + free(map->values); + map->values = NULL; + free(map); + } +} +json_map_int_int *make_json_map_int_int(yajl_val src, const struct parser_context *ctx, parser_error *err) { + json_map_int_int *ret = NULL; + if (src != NULL && YAJL_GET_OBJECT(src) != NULL) { + size_t i; + size_t len = YAJL_GET_OBJECT(src)->len; + ret = safe_malloc(sizeof(*ret)); + ret->len = len; + ret->keys = safe_malloc((len + 1) * sizeof(int)); + ret->values = safe_malloc((len + 1) * sizeof(int)); + for (i = 0; i < len; i++) { + const char *srckey = YAJL_GET_OBJECT(src)->keys[i]; + yajl_val srcval = YAJL_GET_OBJECT(src)->values[i]; + + if (srckey != NULL) { + int invalid; + invalid = common_safe_int(srckey, &(ret->keys[i])); + if (invalid) { + if (*err == NULL && asprintf(err, "Invalid key '%s' with type 'int': %s", srckey, strerror(-invalid)) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_int_int(ret); + return NULL; + } + } + + if (srcval != NULL) { + int invalid; + if (!YAJL_IS_NUMBER(srcval)) { + if (*err == NULL && asprintf(err, "Invalid value with type 'int' for key '%s'", srckey) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_int_int(ret); + return NULL; + } + invalid = common_safe_int(YAJL_GET_NUMBER(srcval), &(ret->values[i])); + if (invalid) { + if (*err == NULL && asprintf(err, "Invalid value with type 'int' for key '%s': %s", srckey, strerror(-invalid)) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_int_int(ret); + return NULL; + } + } + } + } + return ret; +} +int append_json_map_int_int(json_map_int_int *map, int key, int val) { + size_t len; + int *keys = NULL; + int *vals = NULL; + + if (map == NULL) { + return -1; + } + + if ((SIZE_MAX / sizeof(int) - 1) < map->len) { + return -1; + } + + len = map->len + 1; + keys = safe_malloc(len * sizeof(int)); + vals = safe_malloc(len * sizeof(int)); + + if (map->len) { + if (memcpy_s(keys, len * sizeof(int), map->keys, map->len * sizeof(int)) != EOK) { + free(keys); + free(vals); + return -1; + } + if (memcpy_s(vals, len * sizeof(int), map->values, map->len * sizeof(int)) != EOK) { + free(keys); + free(vals); + return -1; + } + } + free(map->keys); + map->keys = keys; + free(map->values); + map->values = vals; + map->keys[map->len] = key; + map->values[map->len] = val; + + map->len++; + return 0; +} + +yajl_gen_status gen_json_map_int_bool(void *ctx, const json_map_int_bool *map, const struct parser_context *ptx, parser_error *err) { + yajl_gen_status stat = yajl_gen_status_ok; + yajl_gen g = (yajl_gen) ctx; + size_t len = 0, i = 0; + if (map != NULL) { + len = map->len; + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 0); + } + stat = yajl_gen_map_open((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + + } + for (i = 0; i < len; i++) { + char numstr[MAX_NUM_STR_LEN]; + int nret; + nret = sprintf_s(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); + if (nret < 0) { + if (!*err && asprintf(err, "Error to print string") < 0) { + *(err) = safe_strdup("error allocating memory"); + } + return yajl_gen_in_error_state; + } + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)numstr, strlen(numstr)); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + stat = yajl_gen_bool((yajl_gen)g, (int)(map->values[i])); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + } + + stat = yajl_gen_map_close((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 1); + } + return yajl_gen_status_ok; +} + +void free_json_map_int_bool(json_map_int_bool *map) { + if (map != NULL) { + size_t i; + for (i = 0; i < map->len; i++) { + // No need to free key for type int + // No need to free value for type bool + } + free(map->keys); + map->keys = NULL; + free(map->values); + map->values = NULL; + free(map); + } +} +json_map_int_bool *make_json_map_int_bool(yajl_val src, const struct parser_context *ctx, parser_error *err) { + json_map_int_bool *ret = NULL; + if (src != NULL && YAJL_GET_OBJECT(src) != NULL) { + size_t i; + size_t len = YAJL_GET_OBJECT(src)->len; + ret = safe_malloc(sizeof(*ret)); + ret->len = len; + ret->keys = safe_malloc((len + 1) * sizeof(int)); + ret->values = safe_malloc((len + 1) * sizeof(bool)); + for (i = 0; i < len; i++) { + const char *srckey = YAJL_GET_OBJECT(src)->keys[i]; + yajl_val srcval = YAJL_GET_OBJECT(src)->values[i]; + + if (srckey != NULL) { + int invalid; + invalid = common_safe_int(srckey, &(ret->keys[i])); + if (invalid) { + if (*err == NULL && asprintf(err, "Invalid key '%s' with type 'int': %s", srckey, strerror(-invalid)) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_int_bool(ret); + return NULL; + } + } + + if (srcval != NULL) { + if (YAJL_IS_TRUE(srcval)) { + ret->values[i] = true; + } else if (YAJL_IS_FALSE(srcval)) { + ret->values[i] = false; + } else { + if (*err == NULL && asprintf(err, "Invalid value with type 'bool' for key '%s'", srckey) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_int_bool(ret); + return NULL; + } + } + } + } + return ret; +} +int append_json_map_int_bool(json_map_int_bool *map, int key, bool val) { + size_t len; + int *keys = NULL; + bool *vals = NULL; + + if (map == NULL) { + return -1; + } + + if ((SIZE_MAX / sizeof(int) - 1) < map->len || (SIZE_MAX / sizeof(bool) - 1) < map->len) { + return -1; + } + + len = map->len + 1; + keys = safe_malloc(len * sizeof(int)); + vals = safe_malloc(len * sizeof(bool)); + + if (map->len) { + if (memcpy_s(keys, len * sizeof(int), map->keys, map->len * sizeof(int)) != EOK) { + free(keys); + free(vals); + return -1; + } + if (memcpy_s(vals, len * sizeof(bool), map->values, map->len * sizeof(bool)) != EOK) { + free(keys); + free(vals); + return -1; + } + } + free(map->keys); + map->keys = keys; + free(map->values); + map->values = vals; + map->keys[map->len] = key; + map->values[map->len] = val; + + map->len++; + return 0; +} + +yajl_gen_status gen_json_map_int_string(void *ctx, const json_map_int_string *map, const struct parser_context *ptx, parser_error *err) { + yajl_gen_status stat = yajl_gen_status_ok; + yajl_gen g = (yajl_gen) ctx; + size_t len = 0, i = 0; + if (map != NULL) { + len = map->len; + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 0); + } + stat = yajl_gen_map_open((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + + } + for (i = 0; i < len; i++) { + char numstr[MAX_NUM_STR_LEN]; + int nret; + nret = sprintf_s(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); + if (nret < 0) { + if (!*err && asprintf(err, "Error to print string") < 0) { + *(err) = safe_strdup("error allocating memory"); + } + return yajl_gen_in_error_state; + } + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)numstr, strlen(numstr)); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)(map->values[i]), strlen(map->values[i])); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + } + + stat = yajl_gen_map_close((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 1); + } + return yajl_gen_status_ok; +} + +void free_json_map_int_string(json_map_int_string *map) { + if (map != NULL) { + size_t i; + for (i = 0; i < map->len; i++) { + // No need to free key for type int + free(map->values[i]); + map->values[i] = NULL; + } + free(map->keys); + map->keys = NULL; + free(map->values); + map->values = NULL; + free(map); + } +} +json_map_int_string *make_json_map_int_string(yajl_val src, const struct parser_context *ctx, parser_error *err) { + json_map_int_string *ret = NULL; + if (src != NULL && YAJL_GET_OBJECT(src) != NULL) { + size_t i; + size_t len = YAJL_GET_OBJECT(src)->len; + ret = safe_malloc(sizeof(*ret)); + ret->len = len; + ret->keys = safe_malloc((len + 1) * sizeof(int)); + ret->values = safe_malloc((len + 1) * sizeof(char *)); + for (i = 0; i < len; i++) { + const char *srckey = YAJL_GET_OBJECT(src)->keys[i]; + yajl_val srcval = YAJL_GET_OBJECT(src)->values[i]; + + if (srckey != NULL) { + int invalid; + invalid = common_safe_int(srckey, &(ret->keys[i])); + if (invalid) { + if (*err == NULL && asprintf(err, "Invalid key '%s' with type 'int': %s", srckey, strerror(-invalid)) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_int_string(ret); + return NULL; + } + } + + if (srcval != NULL) { + if (!YAJL_IS_STRING(srcval)) { + if (*err == NULL && asprintf(err, "Invalid value with type 'string' for key '%s'", srckey) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_int_string(ret); + return NULL; + } + char *str = YAJL_GET_STRING(srcval); + ret->values[i] = safe_strdup(str ? str : ""); + } + } + } + return ret; +} +int append_json_map_int_string(json_map_int_string *map, int key, const char *val) { + size_t len; + int *keys = NULL; + char **vals = NULL; + + if (map == NULL) { + return -1; + } + + if ((SIZE_MAX / sizeof(int) - 1) < map->len || (SIZE_MAX / sizeof(char *) - 1) < map->len) { + return -1; + } + + len = map->len + 1; + keys = safe_malloc(len * sizeof(int)); + vals = safe_malloc(len * sizeof(char *)); + + if (map->len) { + if (memcpy_s(keys, len * sizeof(int), map->keys, map->len * sizeof(int)) != EOK) { + free(keys); + free(vals); + return -1; + } + if (memcpy_s(vals, len * sizeof(char *), map->values, map->len * sizeof(char *)) != EOK) { + free(keys); + free(vals); + return -1; + } + } + free(map->keys); + map->keys = keys; + free(map->values); + map->values = vals; + map->keys[map->len] = key; + map->values[map->len] = safe_strdup(val ? val : ""); + + map->len++; + return 0; +} + +yajl_gen_status gen_json_map_string_int(void *ctx, const json_map_string_int *map, const struct parser_context *ptx, parser_error *err) { + yajl_gen_status stat = yajl_gen_status_ok; + yajl_gen g = (yajl_gen) ctx; + size_t len = 0, i = 0; + if (map != NULL) { + len = map->len; + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 0); + } + stat = yajl_gen_map_open((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + + } + for (i = 0; i < len; i++) { + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)(map->keys[i]), strlen(map->keys[i])); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + stat = map_int(g, map->values[i]); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + } + + stat = yajl_gen_map_close((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 1); + } + return yajl_gen_status_ok; +} + +void free_json_map_string_int(json_map_string_int *map) { + if (map != NULL) { + size_t i; + for (i = 0; i < map->len; i++) { + free(map->keys[i]); + map->keys[i] = NULL; + } + free(map->keys); + map->keys = NULL; + free(map->values); + map->values = NULL; + free(map); + } +} +json_map_string_int *make_json_map_string_int(yajl_val src, const struct parser_context *ctx, parser_error *err) { + json_map_string_int *ret = NULL; + if (src != NULL && YAJL_GET_OBJECT(src) != NULL) { + size_t i; + size_t len = YAJL_GET_OBJECT(src)->len; + ret = safe_malloc(sizeof(*ret)); + ret->len = len; + ret->keys = safe_malloc((len + 1) * sizeof(char *)); + ret->values = safe_malloc((len + 1) * sizeof(int)); + for (i = 0; i < len; i++) { + const char *srckey = YAJL_GET_OBJECT(src)->keys[i]; + yajl_val srcval = YAJL_GET_OBJECT(src)->values[i]; + ret->keys[i] = safe_strdup(srckey ? srckey : ""); + + if (srcval != NULL) { + int invalid; + if (!YAJL_IS_NUMBER(srcval)) { + if (*err == NULL && asprintf(err, "Invalid value with type 'int' for key '%s'", srckey) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_string_int(ret); + return NULL; + } + invalid = common_safe_int(YAJL_GET_NUMBER(srcval), &(ret->values[i])); + if (invalid) { + if (*err == NULL && asprintf(err, "Invalid value with type 'int' for key '%s': %s", srckey, strerror(-invalid)) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_string_int(ret); + return NULL; + } + } + } + } + return ret; +} +int append_json_map_string_int(json_map_string_int *map, const char *key, int val) { + size_t len; + char **keys = NULL; + int *vals = NULL; + + if (map == NULL) { + return -1; + } + + if ((SIZE_MAX / sizeof(char *) - 1) < map->len || (SIZE_MAX / sizeof(int) - 1) < map->len) { + return -1; + } + + len = map->len + 1; + keys = safe_malloc(len * sizeof(char *)); + vals = safe_malloc(len * sizeof(int)); + + if (map->len) { + if (memcpy_s(keys, len * sizeof(char *), map->keys, map->len * sizeof(char *)) != EOK) { + free(keys); + free(vals); + return -1; + } + if (memcpy_s(vals, len * sizeof(int), map->values, map->len * sizeof(int)) != EOK) { + free(keys); + free(vals); + return -1; + } + } + free(map->keys); + map->keys = keys; + free(map->values); + map->values = vals; + map->keys[map->len] = safe_strdup(key ? key : ""); + map->values[map->len] = val; + + map->len++; + return 0; +} + +yajl_gen_status gen_json_map_string_bool(void *ctx, const json_map_string_bool *map, const struct parser_context *ptx, parser_error *err) { + yajl_gen_status stat = yajl_gen_status_ok; + yajl_gen g = (yajl_gen) ctx; + size_t len = 0, i = 0; + if (map != NULL) { + len = map->len; + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 0); + } + stat = yajl_gen_map_open((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + + } + for (i = 0; i < len; i++) { + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)(map->keys[i]), strlen(map->keys[i])); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + stat = yajl_gen_bool((yajl_gen)g, (int)(map->values[i])); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + } + + stat = yajl_gen_map_close((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 1); + } + return yajl_gen_status_ok; +} + +void free_json_map_string_bool(json_map_string_bool *map) { + if (map != NULL) { + size_t i; + for (i = 0; i < map->len; i++) { + free(map->keys[i]); + map->keys[i] = NULL; + // No need to free value for type bool + } + free(map->keys); + map->keys = NULL; + free(map->values); + map->values = NULL; + free(map); + } +} +json_map_string_bool *make_json_map_string_bool(yajl_val src, const struct parser_context *ctx, parser_error *err) { + json_map_string_bool *ret = NULL; + if (src != NULL && YAJL_GET_OBJECT(src) != NULL) { + size_t i; + size_t len = YAJL_GET_OBJECT(src)->len; + ret = safe_malloc(sizeof(*ret)); + ret->len = len; + ret->keys = safe_malloc((len + 1) * sizeof(char *)); + ret->values = safe_malloc((len + 1) * sizeof(bool)); + for (i = 0; i < len; i++) { + const char *srckey = YAJL_GET_OBJECT(src)->keys[i]; + yajl_val srcval = YAJL_GET_OBJECT(src)->values[i]; + ret->keys[i] = safe_strdup(srckey ? srckey : ""); + + if (srcval != NULL) { + if (YAJL_IS_TRUE(srcval)) { + ret->values[i] = true; + } else if (YAJL_IS_FALSE(srcval)) { + ret->values[i] = false; + } else { + if (*err == NULL && asprintf(err, "Invalid value with type 'bool' for key '%s'", srckey) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_string_bool(ret); + return NULL; + } + } + } + } + return ret; +} +int append_json_map_string_bool(json_map_string_bool *map, const char *key, bool val) { + size_t len; + char **keys = NULL; + bool *vals = NULL; + + if (map == NULL) { + return -1; + } + + if ((SIZE_MAX / sizeof(char *) - 1) < map->len || (SIZE_MAX / sizeof(bool) - 1) < map->len) { + return -1; + } + + len = map->len + 1; + keys = safe_malloc(len * sizeof(char *)); + vals = safe_malloc(len * sizeof(bool)); + + if (map->len) { + if (memcpy_s(keys, len * sizeof(char *), map->keys, map->len * sizeof(char *)) != EOK) { + free(keys); + free(vals); + return -1; + } + if (memcpy_s(vals, len * sizeof(bool), map->values, map->len * sizeof(bool)) != EOK) { + free(keys); + free(vals); + return -1; + } + } + free(map->keys); + map->keys = keys; + free(map->values); + map->values = vals; + map->keys[map->len] = safe_strdup(key ? key : ""); + map->values[map->len] = val; + + map->len++; + return 0; +} + +yajl_gen_status gen_json_map_string_string(void *ctx, const json_map_string_string *map, const struct parser_context *ptx, parser_error *err) { + yajl_gen_status stat = yajl_gen_status_ok; + yajl_gen g = (yajl_gen) ctx; + size_t len = 0, i = 0; + if (map != NULL) { + len = map->len; + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 0); + } + stat = yajl_gen_map_open((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + + } + for (i = 0; i < len; i++) { + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)(map->keys[i]), strlen(map->keys[i])); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)(map->values[i]), strlen(map->values[i])); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + } + + stat = yajl_gen_map_close((yajl_gen)g); + if (yajl_gen_status_ok != stat) { + GEN_SET_ERROR_AND_RETURN(stat, err); + } + if (!len && !(ptx->options & OPT_GEN_SIMPLIFY)) { + yajl_gen_config(g, yajl_gen_beautify, 1); + } + return yajl_gen_status_ok; +} + +void free_json_map_string_string(json_map_string_string *map) { + if (map != NULL) { + size_t i; + for (i = 0; i < map->len; i++) { + free(map->keys[i]); + map->keys[i] = NULL; + free(map->values[i]); + map->values[i] = NULL; + } + free(map->keys); + map->keys = NULL; + free(map->values); + map->values = NULL; + free(map); + } +} +json_map_string_string *make_json_map_string_string(yajl_val src, const struct parser_context *ctx, parser_error *err) { + json_map_string_string *ret = NULL; + if (src != NULL && YAJL_GET_OBJECT(src) != NULL) { + size_t i; + size_t len = YAJL_GET_OBJECT(src)->len; + ret = safe_malloc(sizeof(*ret)); + ret->len = len; + ret->keys = safe_malloc((len + 1) * sizeof(char *)); + ret->values = safe_malloc((len + 1) * sizeof(char *)); + for (i = 0; i < len; i++) { + const char *srckey = YAJL_GET_OBJECT(src)->keys[i]; + yajl_val srcval = YAJL_GET_OBJECT(src)->values[i]; + ret->keys[i] = safe_strdup(srckey ? srckey : ""); + + if (srcval != NULL) { + if (!YAJL_IS_STRING(srcval)) { + if (*err == NULL && asprintf(err, "Invalid value with type 'string' for key '%s'", srckey) < 0) { + *(err) = safe_strdup("error allocating memory"); + } + free_json_map_string_string(ret); + return NULL; + } + char *str = YAJL_GET_STRING(srcval); + ret->values[i] = safe_strdup(str ? str : ""); + } + } + } + return ret; +} +int append_json_map_string_string(json_map_string_string *map, const char *key, const char *val) { + size_t len, i; + char **keys = NULL; + char **vals = NULL; + + if (map == NULL) { + return -1; + } + + for (i = 0; i < map->len; i++) { + if (strcmp(map->keys[i], key) == 0) { + free(map->values[i]); + map->values[i] = safe_strdup(val ? val : ""); + return 0; + } + } + + if ((SIZE_MAX / sizeof(char *) - 1) < map->len) { + return -1; + } + + len = map->len + 1; + keys = safe_malloc(len * sizeof(char *)); + vals = safe_malloc(len * sizeof(char *)); + + if (map->len) { + if (memcpy_s(keys, len * sizeof(char *), map->keys, map->len * sizeof(char *)) != EOK) { + free(keys); + free(vals); + return -1; + } + if (memcpy_s(vals, len * sizeof(char *), map->values, map->len * sizeof(char *)) != EOK) { + free(keys); + free(vals); + return -1; + } + } + free(map->keys); + map->keys = keys; + free(map->values); + map->values = vals; + map->keys[map->len] = safe_strdup(key ? key : ""); + map->values[map->len] = safe_strdup(val ? val : ""); + + map->len++; + return 0; +} + +char *json_marshal_string(const char *str, size_t strlen, const struct parser_context *ctx, parser_error *err) +{ + yajl_gen g = NULL; + struct parser_context tmp_ctx = { 0 }; + const unsigned char *gen_buf = NULL; + char *json_buf = NULL; + size_t gen_len = 0; + yajl_gen_status stat; + + if (str == NULL || err == NULL) + return NULL; + + *err = NULL; + if (ctx == NULL) { + ctx = (const struct parser_context *)(&tmp_ctx); + } + + if (!json_gen_init(&g, ctx)) { + *err = safe_strdup("Json_gen init failed"); + goto out; + } + stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)str, strlen); + if (yajl_gen_status_ok != stat) { + if (asprintf(err, "error generating json, errcode: %d", (int)stat) < 0) { + *err = safe_strdup("error allocating memory"); + } + goto free_out; + } + yajl_gen_get_buf(g, &gen_buf, &gen_len); + if (gen_buf == NULL) { + *err = safe_strdup("Error to get generated json"); + goto free_out; + } + + json_buf = safe_malloc(gen_len + 1); + if (memcpy_s(json_buf, gen_len + 1, gen_buf, gen_len) != EOK) { + *err = safe_strdup("Error to memcpy json"); + free(json_buf); + json_buf = NULL; + goto free_out; + } + json_buf[gen_len] = '\\0'; + +free_out: + yajl_gen_clear(g); + yajl_gen_free(g); +out: + return json_buf; +} + +''' diff --git a/src/json/schema/src/common_h.py b/src/json/schema/src/common_h.py new file mode 100644 index 0000000..e2e04ce --- /dev/null +++ b/src/json/schema/src/common_h.py @@ -0,0 +1,198 @@ +# -*- coding: utf-8 -*- +''' +Description: commom header file +Interface: None +History: 2019-06-17 +''' +# - Copyright (C) Huawei Technologies., 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. +# - Description: generate json +# - Author: tanyifeng +# - Create: 2018-04-25 +#!/usr/bin/python -Es + +""" +Description: json common c code +Interface: None +History: 2019-06-18 +Purpose: defined the common tool function for parse json +Defined the CODE global variable to hold the c code +""" +# - Defined the CODE global variable to hold the c code +CODE = '''// Auto generated file. Do not edit! +# ifndef _JSON_COMMON_H +# define _JSON_COMMON_H + +# include +# include +# include +# include +# include +# include +# include +# include "securec.h" + +# ifdef __cplusplus +extern "C" { +# endif + +# undef linux + +//options to report error if there is unknown key found in json +# define OPT_PARSE_STRICT 0x01 +//options to generate all key and value +# define OPT_GEN_KAY_VALUE 0x02 +//options to generate simplify(no indent) json string +# define OPT_GEN_SIMPLIFY 0x04 +//options not to validate utf8 data +# define OPT_GEN_NO_VALIDATE_UTF8 0x08 + +# define GEN_SET_ERROR_AND_RETURN(stat, err) { \\ + if (*(err) == NULL) {\\ + if (asprintf(err, "%s: %s: %d: error generating json, errcode: %u", __FILE__, __func__, __LINE__, stat) < 0) { \\ + *(err) = safe_strdup("error allocating memory"); \\ + } \\ + }\\ + return stat; \\ +} + +typedef char *parser_error; + +struct parser_context { + unsigned int options; + FILE *stderr; +}; + +yajl_gen_status map_uint(void *ctx, long long unsigned int num); + +yajl_gen_status map_int(void *ctx, long long int num); + +bool json_gen_init(yajl_gen *g, const struct parser_context *ctx); + +yajl_val get_val(yajl_val tree, const char *name, yajl_type type); + +void *safe_malloc(size_t size); + +int common_safe_double(const char *numstr, double *converted); + +int common_safe_uint8(const char *numstr, uint8_t *converted); + +int common_safe_uint16(const char *numstr, uint16_t *converted); + +int common_safe_uint32(const char *numstr, uint32_t *converted); + +int common_safe_uint64(const char *numstr, uint64_t *converted); + +int common_safe_uint(const char *numstr, unsigned int *converted); + +int common_safe_int8(const char *numstr, int8_t *converted); + +int common_safe_int16(const char *numstr, int16_t *converted); + +int common_safe_int32(const char *numstr, int32_t *converted); + +int common_safe_int64(const char *numstr, int64_t *converted); + +int common_safe_int(const char *numstr, int *converted); + +char *safe_strdup(const char *src); + +typedef struct { + int *keys; + int *values; + size_t len; +} json_map_int_int; + +void free_json_map_int_int(json_map_int_int *map); + +json_map_int_int *make_json_map_int_int(yajl_val src, const struct parser_context *ctx, parser_error *err); + +yajl_gen_status gen_json_map_int_int(void *ctx, const json_map_int_int *map, const struct parser_context *ptx, parser_error *err); + +int append_json_map_int_int(json_map_int_int *map, int key, int val); + +typedef struct { + int *keys; + bool *values; + size_t len; +} json_map_int_bool; + +void free_json_map_int_bool(json_map_int_bool *map); + +json_map_int_bool *make_json_map_int_bool(yajl_val src, const struct parser_context *ctx, parser_error *err); + +yajl_gen_status gen_json_map_int_bool(void *ctx, const json_map_int_bool *map, const struct parser_context *ptx, parser_error *err); + +int append_json_map_int_bool(json_map_int_bool *map, int key, bool val); + +typedef struct { + int *keys; + char **values; + size_t len; +} json_map_int_string; + +void free_json_map_int_string(json_map_int_string *map); + +json_map_int_string *make_json_map_int_string(yajl_val src, const struct parser_context *ctx, parser_error *err); + +yajl_gen_status gen_json_map_int_string(void *ctx, const json_map_int_string *map, const struct parser_context *ptx, parser_error *err); + +int append_json_map_int_string(json_map_int_string *map, int key, const char *val); + +typedef struct { + char **keys; + int *values; + size_t len; +} json_map_string_int; + +void free_json_map_string_int(json_map_string_int *map); + +json_map_string_int *make_json_map_string_int(yajl_val src, const struct parser_context *ctx, parser_error *err); + +yajl_gen_status gen_json_map_string_int(void *ctx, const json_map_string_int *map, const struct parser_context *ptx, parser_error *err); + +int append_json_map_string_int(json_map_string_int *map, const char *key, int val); + +typedef struct { + char **keys; + bool *values; + size_t len; +} json_map_string_bool; + +void free_json_map_string_bool(json_map_string_bool *map); + +json_map_string_bool *make_json_map_string_bool(yajl_val src, const struct parser_context *ctx, parser_error *err); + +yajl_gen_status gen_json_map_string_bool(void *ctx, const json_map_string_bool *map, const struct parser_context *ptx, parser_error *err); + +int append_json_map_string_bool(json_map_string_bool *map, const char *key, bool val); + +typedef struct { + char **keys; + char **values; + size_t len; +} json_map_string_string; + +void free_json_map_string_string(json_map_string_string *map); + +json_map_string_string *make_json_map_string_string(yajl_val src, const struct parser_context *ctx, parser_error *err); + +yajl_gen_status gen_json_map_string_string(void *ctx, const json_map_string_string *map, const struct parser_context *ptx, parser_error *err); + +int append_json_map_string_string(json_map_string_string *map, const char *key, const char *val); + +char *json_marshal_string(const char *str, size_t strlen, const struct parser_context *ctx, parser_error *err); + +# ifdef __cplusplus +} +# endif + +# endif +''' diff --git a/src/json/schema/src/generate.py b/src/json/schema/src/generate.py new file mode 100644 index 0000000..2d9485c --- /dev/null +++ b/src/json/schema/src/generate.py @@ -0,0 +1,802 @@ +# -*- coding: utf-8 -*- +''' +Description: header class and functions +Interface: None +History: 2019-06-17 +''' +# - Copyright (C) Huawei Technologies., 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. +# - Description: generate json +# - Author: tanyifeng +# - Create: 2018-04-25 +#!/usr/bin/python -Es + +import traceback +import os +import sys +import json +import fcntl +import argparse + +from collections import OrderedDict +import helpers +import headers +import sources +import common_h +import common_c + +# - json suffix +JSON_SUFFIX = ".json" + +''' +Description: ref suffix +Interface: ref_suffix +History: 2019-06-17 +''' +# - Description: ref suffix +REF_SUFFIX = "_json" + +''' +Description: root paths +Interface: rootpaths +History: 2019-06-17 +''' +class MyRoot(object): + ''' + Description: Store schema information + Interface: None + History: 2019-06-17 + ''' + def __init__(self, root_path): + self.root_path = root_path + + def get_repr(self): + ''' + Description: Store schema information + Interface: None + History: 2019-06-17 + ''' + return "{root_path:(%s)}" % (self.root_path) + + def get_path(self): + ''' + Description: Store schema information + Interface: None + History: 2019-06-17 + ''' + return self.root_path + + +def trimJsonSuffix(name): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + if name.endswith(JSON_SUFFIX) or name.endswith(REF_SUFFIX): + name = name[:-len(JSON_SUFFIX)] + return helpers.convertToCStyle(name.replace('.', '_').replace('-', '_')) + + +def getPrefixPackage(filepath, rootpath): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + realpath = os.path.realpath(filepath) + + if realpath.startswith(rootpath) and len(realpath) > len(rootpath): + return helpers.convertToCStyle(os.path.dirname(realpath)[(len(rootpath) + 1):]) + else: + raise RuntimeError('schema path \"%s\" is not in scope of root path \"%s\"' \ + % (realpath, rootpath)) + + +def getPrefixFromFile(filepath): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + prefix_file = trimJsonSuffix(os.path.basename(filepath)) + root_path = MyRoot.root_path + prefix_package = getPrefixPackage(filepath, root_path) + prefix = prefix_file if prefix_package == "" else prefix_package + "_" + prefix_file + return prefix + +def schemaFromFile(filepath, srcpath): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + schemapath = helpers.FilePath(filepath) + prefix = getPrefixFromFile(schemapath.name) + header = helpers.FilePath(os.path.join(srcpath, prefix + ".h")) + source = helpers.FilePath(os.path.join(srcpath, prefix + ".c")) + schema_info = helpers.SchemaInfo(schemapath, header, source, prefix, srcpath) + return schema_info + +def makeRefName(refname, reffile): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + prefix = getPrefixFromFile(reffile) + if refname == "" or prefix.endswith(refname): + return prefix + return prefix + "_" + helpers.convertToCStyle(refname) + +def splitRefName(ref): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + tmp_f, tmp_r = ref.split("#/") if '#/' in ref else (ref, "") + return tmp_f, tmp_r + +def merge(children): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + subchildren = [] + for i in children: + for j in i.children: + subchildren.append(j) + + return subchildren + +# BASIC_TYPES include all basic types +BASIC_TYPES = ( + "byte", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "UID", "GID", + "bytePointer", "doublePointer", "int8Pointer", "int16Pointer", "int32Pointer", "int64Pointer", + "uint8Pointer", "uint16Pointer", "uint32Pointer", "uint64Pointer", "ArrayOfStrings", + "booleanPointer" +) + +def judgeSupportedType(typ): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + return typ in ("integer", "boolean", "string", "double") or typ in BASIC_TYPES + +def getRefSubref(src, subref): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + cur = src + subrefname = "" + for j in subref.split('/'): + subrefname = j + if j in BASIC_TYPES: + return src, {"type": j}, subrefname + cur = cur[j] + + return src, cur, subrefname + +def getRefRoot(schema_info, src, ref, curfile): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + refname = "" + tmp_f, tmp_r = splitRefName(ref) + + if tmp_f == "": + cur = src + else: + realpath = os.path.realpath(os.path.join(os.path.dirname(curfile), tmp_f)) + curfile = realpath + + subschema = schemaFromFile(realpath, schema_info.filesdir) + if schema_info.refs is None: + schema_info.refs = {} + schema_info.refs[subschema.header.basename] = subschema + with open(realpath) as i: + cur = src = json.loads(i.read()) + subcur = cur + if tmp_r != "": + src, subcur, refname = getRefSubref(src, tmp_r) + + if 'type' not in subcur and '$ref' in subcur: + subf, subr = splitRefName(subcur['$ref']) + if subf == "": + src, subcur, refname = getRefSubref(src, subr) + if 'type' not in subcur: + raise RuntimeError("Not support reference of nesting more than 2 level: ", ref) + else: + return getRefRoot(schema_info, src, subcur['$ref'], curfile) + return src, subcur, curfile, makeRefName(refname, curfile) + +def getTypePatternInCur(cur, schema_info, src, curfile): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + # pattern of key: + # '.{1,}' represents type 'string', + # '.{2,}' represents type 'integer' + if '.{2,}' in cur['patternProperties']: + map_key = 'Int' + else: + map_key = 'String' + for i, value in enumerate(cur['patternProperties'].values()): + # only use the first value + if i == 0: + if 'type' in value: + val = value["type"] + else: + dummy_subsrc, subcur, dummy_subcurfile, dummy_subrefname = getRefRoot( + schema_info, src, value['$ref'], curfile) + val = subcur['type'] + break + + mapKey = { + 'object': 'Object', + 'string': 'String', + 'integer': 'Int', + 'boolean': 'Bool' + }[val] + map_val = mapKey + + typ = 'map' + map_key + map_val + return typ + +class GenerateNodeInfo(object): + ''' + Description: Store schema information + Interface: None + History: 2019-06-17 + ''' + def __init__(self, schema_info, name, cur, curfile): + self.schema_info = schema_info + self.name = name + self.cur = cur + self.curfile = curfile + + def get_repr(self): + ''' + Description: Store schema information + Interface: None + History: 2019-06-17 + ''' + return "{schema_info:(%s) name:(%s) cur:(%s) curfile:(%s)}" \ + % (self.schema_info, self.name, self.cur, self.curfile) + + def get_name(self): + ''' + Description: Store schema information + Interface: None + History: 2019-06-17 + ''' + return self.name + +def generateAllofArrayTypNode(node_info, src, typ, refname): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + schema_info = node_info.schema_info + name = node_info.name + cur = node_info.cur + curfile = node_info.curfile + subtyp = None + subtypobj = None + required = None + children = merge(parseList(schema_info, name, src, cur["items"]['allOf'], curfile)) + subtyp = children[0].typ + subtypobj = children + return helpers.Unite(name, + typ, + children, + subtyp=subtyp, + subtypobj=subtypobj, + subtypname=refname, + required=required), src + +def generateAnyofArrayTypNode(node_info, src, typ, refname): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + schema_info = node_info.schema_info + name = node_info.name + cur = node_info.cur + curfile = node_info.curfile + subtyp = None + subtypobj = None + required = None + anychildren = parseList(schema_info, name, src, cur["items"]['anyOf'], curfile) + subtyp = anychildren[0].typ + children = anychildren[0].children + subtypobj = children + refname = anychildren[0].subtypname + return helpers.Unite(name, + typ, + children, + subtyp=subtyp, + subtypobj=subtypobj, + subtypname=refname, + required=required), src + +def generateRefArrayTypNode(node_info, src, typ, refname): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + schema_info = node_info.schema_info + name = node_info.name + cur = node_info.cur + curfile = node_info.curfile + + item_type, src = resolveType(schema_info, name, src, cur["items"], curfile) + ref_file, subref = splitRefName(cur['items']['$ref']) + if ref_file == "": + src, dummy_subcur, subrefname = getRefSubref(src, subref) + refname = makeRefName(subrefname, curfile) + else: + refname = item_type.subtypname + return helpers.Unite(name, + typ, + None, + subtyp=item_type.typ, + subtypobj=item_type.children, + subtypname=refname, + required=item_type.required), src + +def generateTypeArrayTypNode(node_info, src, typ, refname): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + schema_info = node_info.schema_info + name = node_info.name + cur = node_info.cur + curfile = node_info.curfile + + item_type, src = resolveType(schema_info, name, src, cur["items"], curfile) + return helpers.Unite(name, + typ, + None, + subtyp=item_type.typ, + subtypobj=item_type.children, + subtypname=refname, + required=item_type.required), src + + +def generateArrayTypNode(node_info, src, typ, refname): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + cur = node_info.cur + + if 'allOf' in cur["items"]: + return generateAllofArrayTypNode(node_info, src, typ, refname) + elif 'anyOf' in cur["items"]: + return generateAnyofArrayTypNode(node_info, src, typ, refname) + elif '$ref' in cur["items"]: + return generateRefArrayTypNode(node_info, src, typ, refname) + elif 'type' in cur["items"]: + return generateTypeArrayTypNode(node_info, src, typ, refname) + return None + +def generateObjTypNode(node_info, src, typ, refname): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + schema_info = node_info.schema_info + name = node_info.name + cur = node_info.cur + curfile = node_info.curfile + children = None + subtyp = None + subtypobj = None + required = None + + if 'allOf' in cur: + children = merge(parseList(schema_info, name, src, cur['allOf'], curfile)) + elif 'anyOf' in cur: + children = parseList(schema_info, name, src, cur['anyOf'], curfile) + elif 'patternProperties' in cur: + children = parseProperties(schema_info, name, src, cur, curfile) + children[0].name = children[0].name.replace('_{1,}', 'element').replace('_{2,}', \ + 'element') + children[0].fixname = "values" + if helpers.validBasicMapName(children[0].typ): + children[0].name = helpers.makeBasicMapName(children[0].typ) + else: + children = parseProperties(schema_info, name, src, cur, curfile) \ + if 'properties' in cur else None + if 'required' in cur: + required = cur['required'] + return helpers.Unite(name,\ + typ,\ + children,\ + subtyp=subtyp,\ + subtypobj=subtypobj,\ + subtypname=refname,\ + required=required), src + +def getTypNotOneof(schema_info, src, cur, curfile): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + if 'patternProperties' in cur: + typ = getTypePatternInCur(cur, schema_info, src, curfile) + elif "type" in cur: + typ = cur["type"] + else: + typ = "object" + + return typ + + +def resolveType(schema_info, name, src, cur, curfile): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + children = None + subtyp = None + subtypobj = None + required = None + refname = None + + if '$ref' in cur: + src, cur, curfile, refname = getRefRoot(schema_info, src, cur['$ref'], curfile) + + if "oneOf" in cur: + cur = cur['oneOf'][0] + if '$ref' in cur: + return resolveType(schema_info, name, src, cur, curfile) + else: + typ = cur['type'] + else: + typ = getTypNotOneof(schema_info, src, cur, curfile) + + node_info = GenerateNodeInfo(schema_info, name, cur, curfile) + + if helpers.validBasicMapName(typ): + pass + elif typ == 'array': + return generateArrayTypNode(node_info, src, typ, refname) + elif typ == 'object' or typ == 'mapStringObject': + return generateObjTypNode(node_info, src, typ, refname) + elif typ == 'ArrayOfStrings': + typ = 'array' + subtyp = 'string' + children = subtypobj = None + else: + if not judgeSupportedType(typ): + raise RuntimeError("Invalid schema type: %s" % typ) + children = None + + return helpers.Unite(name, + typ, + children, + subtyp=subtyp, + subtypobj=subtypobj, + subtypname=refname, + required=required), src + + +def parseList(schema_info, name, schema, objs, curfile): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + obj = [] + index = 0 + for i in objs: + generated_name = helpers.CombinationName( \ + i['$ref'].split("/")[-1]) if '$ref' in i else helpers.CombinationName(name.name + str(index)) + node, _ = resolveType(schema_info, generated_name, schema, i, curfile) + if node: + obj.append(node) + index += 1 + if not obj: + obj = None + return obj + + +def parseDictionary(schema_info, name, schema, objs, curfile): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + obj = [] + for i in objs: + node, _ = resolveType(schema_info, name.append(i), schema, objs[i], curfile) + if node: + obj.append(node) + if not obj: + obj = None + return obj + + +def parseProperties(schema_info, name, schema, props, curfile): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + if 'definitions' in props: + return parseDictionary(schema_info, name, schema, props['definitions'], curfile) + if 'patternProperties' in props: + return parseDictionary(schema_info, name, schema, props['patternProperties'], curfile) + return parseDictionary(schema_info, name, schema, props['properties'], curfile) + +def handleTypeNotInSchema(schema_info, schema, prefix): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + required = None + if 'definitions' in schema: + return helpers.Unite( \ + helpers.CombinationName("definitions"), 'definitions', \ + parseProperties(schema_info, helpers.CombinationName(""), schema, schema, \ + schema_info.name.name), None, None, None, None) + else: + if len(schema) > 1: + print('More than one element found in schema') + return None + value_nodes = [] + for value in schema: + if 'required' in schema[value]: + required = schema[value]['required'] + childrens = parseProperties(schema_info, helpers.CombinationName(""), schema[value], \ + schema[value], schema_info.name.name) + value_node = helpers.Unite(helpers.CombinationName(prefix), 'object', childrens, None, None, \ + None, required) + value_nodes.append(value_node) + return helpers.Unite(helpers.CombinationName("definitions"), 'definitions', value_nodes, None, None, \ + None, None) + +def parseSchema(schema_info, schema, prefix): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + required = None + if 'type' not in schema: + return handleTypeNotInSchema(schema_info, schema, prefix) + + if 'type' not in schema: + print("No 'type' defined in schema") + return prefix, None + + if 'object' in schema['type']: + if 'required' in schema: + required = schema['required'] + return helpers.Unite( + helpers.CombinationName(prefix), 'object', + parseProperties(schema_info, helpers.CombinationName(""), schema, schema, schema_info.name.name), \ + None, None, None, required) + elif 'array' in schema['type']: + item_type, _ = resolveType(schema_info, helpers.CombinationName(""), schema['items'], \ + schema['items'], schema_info.name.name) + return helpers.Unite(helpers.CombinationName(prefix), 'array', None, item_type.typ, \ + item_type.children, None, item_type.required) + else: + print("Not supported type '%s'") % schema['type'] + return prefix, None + + +def expand(tree, structs, visited): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + if tree.children is not None: + for i in tree.children: + if tree.subtypname: + i.subtypname = "from_ref" + expand(i, structs, visited=visited) + if tree.subtypobj is not None: + for i in tree.subtypobj: + expand(i, structs, visited=visited) + + if tree.typ == 'array' and helpers.validBasicMapName(tree.subtyp): + name = helpers.CombinationName(tree.name + "_element") + node = helpers.Unite(name, tree.subtyp, None) + expand(node, structs, visited) + + id_ = "%s:%s" % (tree.name, tree.typ) + if id_ not in visited.keys(): + structs.append(tree) + visited[id_] = tree + + return structs + + +def reflection(schema_info, gen_ref): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + with open(schema_info.header.name, "w") as \ + header_file, open(schema_info.source.name, "w") as source_file: + fcntl.flock(header_file, fcntl.LOCK_EX) + fcntl.flock(source_file, fcntl.LOCK_EX) + + with open(schema_info.name.name) as schema_file: + schema_json = json.loads(schema_file.read(), object_pairs_hook=OrderedDict) + try: + tree = parseSchema(schema_info, schema_json, schema_info.prefix) + if tree is None: + print("Failed parse schema") + sys.exit(1) + structs = expand(tree, [], {}) + headers.headerReflection(structs, schema_info, header_file) + sources.sourceReflection(structs, schema_info, source_file, tree.typ) + except RuntimeError: + traceback.print_exc() + print("Failed to parse schema file: %s") % schema_info.name.name + sys.exit(1) + finally: + pass + + fcntl.flock(source_file, fcntl.LOCK_UN) + fcntl.flock(header_file, fcntl.LOCK_UN) + + if gen_ref is True: + if schema_info.refs: + for reffile in schema_info.refs.values(): + reflection(reffile, True) + + +def generateCommonFiles(out): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + print(out, " gao\n") + with open(os.path.join(out, 'json_common.h'), "w") as \ + header_file, open(os.path.join(out, 'json_common.c'), "w") as source_file: + fcntl.flock(header_file, fcntl.LOCK_EX) + fcntl.flock(source_file, fcntl.LOCK_EX) + + header_file.write(common_h.CODE) + source_file.write(common_c.CODE) + + fcntl.flock(source_file, fcntl.LOCK_UN) + fcntl.flock(header_file, fcntl.LOCK_UN) + +def handlerSingleFile(args, srcpath, gen_ref, schemapath): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + if not os.path.exists(schemapath.name) or not os.path.exists(srcpath.name): + print('Path %s is not exist') % schemapath.name + sys.exit(1) + + if os.path.isdir(schemapath.name): + if args.recursive is True: + # recursively parse schema + for dirpath, dummy_dirnames, files in os.walk(schemapath.name): + for target_file in files: + if target_file.endswith(JSON_SUFFIX): + schema_info = schemaFromFile(os.path.join(dirpath, target_file), \ + srcpath.name) + reflection(schema_info, gen_ref) + else: + # only parse files in current direcotory + for target_file in os.listdir(schemapath.name): + fullpath = os.path.join(schemapath.name, target_file) + if fullpath.endswith(JSON_SUFFIX) and os.path.isfile(fullpath): + schema_info = schemaFromFile(fullpath, srcpath.name) + reflection(schema_info, gen_ref) + else: + if schemapath.name.endswith(JSON_SUFFIX): + schema_info = schemaFromFile(schemapath.name, srcpath.name) + reflection(schema_info, gen_ref) + else: + print('File %s is not ends with .json') % schemapath.name + + +def handlerFiles(args, srcpath): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + for path in args.path: + gen_ref = args.gen_ref + schemapath = helpers.FilePath(path) + handlerSingleFile(args, srcpath, gen_ref, schemapath) + +def main(): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + parser = argparse.ArgumentParser(prog='generate.py', + usage='%(prog)s [options] path [path ...]', + description='Generate C header and source from json-schema') + parser.add_argument('path', nargs='+', help='File or directory to parse') + parser.add_argument( + '--root', + required=True, + help= + 'All schema files must be placed in root directory or sub-directory of root," \ + " and naming of C variables is started from this path' + ) + parser.add_argument('--gen-common', + action='store_true', + help='Generate json_common.c and json_common.h') + parser.add_argument('--gen-ref', + action='store_true', + help='Generate reference file defined in schema with key \"$ref\"') + parser.add_argument('-r', + '--recursive', + action='store_true', + help='Recursively generate all schema files in directory') + parser.add_argument( + '--out', + help='Specify a directory to save C header and source(default is current directory)') + args = parser.parse_args() + + if not args.root: + print('Missing root path, see help') + sys.exit(1) + + root_path = os.path.realpath(args.root) + if not os.path.exists(root_path): + print('Root %s is not exist') % args.root + sys.exit(1) + + MyRoot.root_path = root_path + + if args.out: + srcpath = helpers.FilePath(args.out) + else: + srcpath = helpers.FilePath(os.getcwd()) + if not os.path.exists(srcpath.name): + os.makedirs(srcpath.name) + + if args.gen_common: + generateCommonFiles(srcpath.name) + handlerFiles(args, srcpath) + +if __name__ == "__main__": + main() diff --git a/src/json/schema/src/headers.py b/src/json/schema/src/headers.py new file mode 100644 index 0000000..ba275a3 --- /dev/null +++ b/src/json/schema/src/headers.py @@ -0,0 +1,197 @@ +# -*- coding: utf-8 -*- +''' +Description: header class and functions +Interface: None +History: 2019-06-17 +''' +# - Copyright (C) Huawei Technologies., 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. +# - Description: generate json +# - Author: tanyifeng +# - Create: 2018-04-25 +#!/usr/bin/python -Es +import helpers + +def appendHeaderArray(obj, header, prefix): + ''' + Description: Write c header file of array + Interface: None + History: 2019-06-17 + ''' + if not obj.subtypobj or obj.subtypname: + return + header.write("typedef struct {\n") + for i in obj.subtypobj: + if i.typ == 'array': + c_typ = helpers.getPrefixPointer(i.name, i.subtyp, prefix) or \ + helpers.getMapCTypes(i.subtyp) + if i.subtypobj is not None: + c_typ = helpers.getNameSubstr(i.name, prefix) + + if not helpers.judgeComplex(i.subtyp): + header.write(" %s%s*%s;\n" % (c_typ, " " if '*' not in c_typ else "", \ + i.fixname)) + else: + header.write(" %s **%s;\n" % (c_typ, i.fixname)) + header.write(" size_t %s;\n\n" % (i.fixname + "_len")) + else: + c_typ = helpers.getPrefixPointer(i.name, i.typ, prefix) or \ + helpers.getMapCTypes(i.typ) + header.write(" %s%s%s;\n" % (c_typ, " " if '*' not in c_typ else "", i.fixname)) + typename = helpers.getNameSubstr(obj.name, prefix) + header.write("}\n%s;\n\n" % typename) + header.write("void free_%s(%s *ptr);\n\n" % (typename, typename)) + header.write("%s *make_%s(yajl_val tree, const struct parser_context *ctx, parser_error *err);"\ + "\n\n" % (typename, typename)) + +def appendHeaderMapStrObj(obj, header, prefix): + ''' + Description: Write c header file of mapStringObject + Interface: None + History: 2019-06-17 + ''' + child = obj.children[0] + header.write("typedef struct {\n") + header.write(" char **keys;\n") + if helpers.validBasicMapName(child.typ): + c_typ = helpers.getPrefixPointer("", child.typ, "") + elif child.subtypname: + c_typ = child.subtypname + else: + c_typ = helpers.getPrefixPointer(child.name, child.typ, prefix) + header.write(" %s%s*%s;\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname)) + header.write(" size_t len;\n") + +def appendHeaderChildArray(child, header, prefix): + ''' + Description: Write c header file of array of child + Interface: None + History: 2019-06-17 + ''' + if helpers.getMapCTypes(child.subtyp) != "": + c_typ = helpers.getMapCTypes(child.subtyp) + elif helpers.validBasicMapName(child.subtyp): + c_typ = '%s *' % helpers.makeBasicMapName(child.subtyp) + elif child.subtypname is not None: + c_typ = child.subtypname + elif child.subtypobj is not None: + c_typ = helpers.getNameSubstr(child.name, prefix) + else: + c_typ = helpers.getPrefixPointer(child.name, child.subtyp, prefix) + + if helpers.validBasicMapName(child.subtyp): + header.write(" %s **%s;\n" % (helpers.makeBasicMapName(child.subtyp), child.fixname)) + elif not helpers.judgeComplex(child.subtyp): + header.write(" %s%s*%s;\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname)) + else: + header.write(" %s%s**%s;\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname)) + header.write(" size_t %s;\n\n" % (child.fixname + "_len")) + +def appendHeaderChildOthers(child, header, prefix): + ''' + Description: Write c header file of others of child + Interface: None + History: 2019-06-17 + ''' + if helpers.getMapCTypes(child.typ) != "": + c_typ = helpers.getMapCTypes(child.typ) + elif helpers.validBasicMapName(child.typ): + c_typ = '%s *' % helpers.makeBasicMapName(child.typ) + elif child.subtypname: + c_typ = helpers.getPrefixPointer(child.subtypname, child.typ, "") + else: + c_typ = helpers.getPrefixPointer(child.name, child.typ, prefix) + header.write(" %s%s%s;\n\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname)) + +def appendTypeCHeader(obj, header, prefix): + ''' + Description: Write c header file + Interface: None + History: 2019-06-17 + ''' + if not helpers.judgeComplex(obj.typ): + return + + if obj.typ == 'array': + appendHeaderArray(obj, header, prefix) + return + + if obj.typ == 'mapStringObject': + if obj.subtypname is not None: + return + appendHeaderMapStrObj(obj, header, prefix) + elif obj.typ == 'object': + if obj.subtypname is not None: + return + header.write("typedef struct {\n") + if obj.children is None: + header.write(" char unuseful;//unuseful definition to avoid empty struct\n") + for i in obj.children or [ ]: + if i.typ == 'array': + appendHeaderChildArray(i, header, prefix) + else: + appendHeaderChildOthers(i, header, prefix) + + typename = helpers.getPrefixName(obj.name, prefix) + header.write("}\n%s;\n\n" % typename) + header.write("void free_%s(%s *ptr);\n\n" % (typename, typename)) + header.write("%s *make_%s(yajl_val tree, const struct parser_context *ctx, parser_error *err)"\ + ";\n\n" % (typename, typename)) + header.write("yajl_gen_status gen_%s(yajl_gen g, const %s *ptr, const struct parser_context "\ + "*ctx, parser_error *err);\n\n" % (typename, typename)) + +def headerReflection(structs, schema_info, header): + ''' + Description: Reflection header files + Interface: None + History: 2019-06-17 + ''' + prefix = schema_info.prefix + header.write("// Generated from %s. Do not edit!\n" % (schema_info.name.basename)) + header.write("#ifndef %s_SCHEMA_H\n" % prefix.upper()) + header.write("#define %s_SCHEMA_H\n\n" % prefix.upper()) + header.write("#include \n") + header.write("#include \n") + header.write("#include \"json_common.h\"\n") + if schema_info.refs: + for ref in schema_info.refs.keys(): + header.write("#include \"%s\"\n" % (ref)) + header.write("\n#ifdef __cplusplus\n") + header.write("extern \"C\" {\n") + header.write("#endif\n\n") + + for i in structs: + appendTypeCHeader(i, header, prefix) + length = len(structs) + toptype = structs[length - 1].typ if length != 0 else "" + if toptype == 'object': + header.write("%s *%s_parse_file(const char *filename, const struct parser_context *ctx, "\ + "parser_error *err);\n\n" % (prefix, prefix)) + header.write("%s *%s_parse_file_stream(FILE *stream, const struct parser_context *ctx, "\ + "parser_error *err);\n\n" % (prefix, prefix)) + header.write("%s *%s_parse_data(const char *jsondata, const struct parser_context *ctx, "\ + "parser_error *err);\n\n" % (prefix, prefix)) + header.write("char *%s_generate_json(const %s *ptr, const struct parser_context *ctx, "\ + "parser_error *err);\n\n" % (prefix, prefix)) + elif toptype == 'array': + header.write("void free_%s(%s_element **ptr, size_t len);\n\n" % (prefix, prefix)) + header.write("%s_element **%s_parse_file(const char *filename, const struct "\ + "parser_context *ctx, parser_error *err, size_t *len);\n\n" % (prefix, prefix)) + header.write("%s_element **%s_parse_file_stream(FILE *stream, const struct "\ + "parser_context *ctx, parser_error *err, size_t *len);\n\n" % (prefix, prefix)) + header.write("%s_element **%s_parse_data(const char *jsondata, const struct "\ + "parser_context *ctx, parser_error *err, size_t *len);\n\n" % (prefix, prefix)) + header.write("char *%s_generate_json(const %s_element **ptr, size_t len, "\ + "const struct parser_context *ctx, parser_error *err);\n\n" % (prefix, prefix)) + + header.write("#ifdef __cplusplus\n") + header.write("}\n") + header.write("#endif\n\n") + header.write("#endif\n\n") diff --git a/src/json/schema/src/helpers.py b/src/json/schema/src/helpers.py new file mode 100644 index 0000000..2b30035 --- /dev/null +++ b/src/json/schema/src/helpers.py @@ -0,0 +1,313 @@ +# -*- coding: utf-8 -*- +''' +Description: helper class and functions +Interface: None +History: 2019-06-17 +''' +# - Copyright (C) Huawei Technologies., 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. +# - Description: generate json +# - Author: tanyifeng +# - Create: 2018-04-25 +#!/usr/bin/python -Es +import os +import sys + +def appendSeparator(subStr): + ''' + Description: append only '_' at last position of subStr + Interface: None + History: 2019-09-20 + ''' + if subStr and subStr[-1] != '_': + subStr.append('_') + +def convertToCStyle(name): + ''' + Description: convert name to linux c format + Interface: None + History: 2019-06-17 + ''' + if name is None or name == "": + return "" + name = name.replace('.', '_').replace('-', '_').replace('/', '_') + subStr = [] + preindex = 0 + index = 0 + for index, char in enumerate(name): + if char == '_': + appendSeparator(subStr) + subStr.append(name[preindex:index].lower()) + preindex = index + 1 + if not char.isupper() and name[preindex].isupper() and \ + name[preindex + 1].isupper(): + appendSeparator(subStr) + subStr.append(name[preindex:index - 1].lower()) + preindex = index - 1 + continue + if char.isupper() and index > 0 and name[index - 1].islower(): + appendSeparator(subStr) + subStr.append(name[preindex:index].lower()) + preindex = index + + if preindex <= index and index >= 0 and name[index] != '_' and \ + preindex != 0: + appendSeparator(subStr) + subStr.append(name[preindex:index + 1].lower()) + result = ''.join(subStr) + return result + +def getMapCTypes(typ): + ''' + Description: Get map c types + Interface: None + History: 2019-06-17 + ''' + map_c_types = { + 'byte': 'uint8_t', + 'string': 'char *', + 'integer': 'int', + 'boolean': 'bool', + 'double': 'double', + 'int8': 'int8_t', + "int16": 'int16_t', + "int32": "int32_t", + "int64": "int64_t", + 'uint8': 'uint8_t', + "uint16": 'uint16_t', + "uint32": "uint32_t", + "uint64": "uint64_t", + "UID": "uid_t", + "GID": "gid_t", + "booleanPointer": "bool *", + 'bytePointer': 'uint8_t *', + 'integerPointer': 'int *', + 'doublePointer': 'double *', + 'int8Pointer': 'int8_t *', + "int16Pointer": 'int16_t *', + "int32Pointer": "int32_t *", + "int64Pointer": "int64_t *", + 'uint8Pointer': 'uint8_t *', + "uint16Pointer": 'uint16_t *', + "uint32Pointer": "uint32_t *", + "uint64Pointer": "uint64_t *", + } + if typ in map_c_types: + return map_c_types[typ] + return "" + +def validBasicMapName(typ): + ''' + Description: Valid basic map name + Interface: None + History: 2019-06-17 + ''' + return typ != 'mapStringObject' and hasattr(typ, 'startswith') and \ + typ.startswith('map') + +def makeBasicMapName(mapname): + ''' + Description: Make basic map name + Interface: None + History: 2019-06-17 + ''' + basic_map_types = ('string', 'int', 'bool') + parts = convertToCStyle(mapname).split('_') + if len(parts) != 3 or parts[0] != 'map' or \ + (parts[1] not in basic_map_types) or \ + (parts[2] not in basic_map_types): + print('Invalid map name: %s') % mapname + sys.exit(1) + return "json_map_%s_%s" % (parts[1], parts[2]) + + +def getNameSubstr(name, prefix): + ''' + Description: Make array name + Interface: None + History: 2019-06-17 + ''' + return "%s_element" % prefix if name is None or name == "" or prefix == name \ + else "%s_%s_element" % (prefix, name) + +def getPrefixName(name, prefix): + ''' + Description: Make name + Interface: None + History: 2019-06-17 + ''' + if name is None or name == "" or prefix.endswith(name): + return "%s" % prefix + if prefix is None or prefix == "" or prefix == name or name.endswith(prefix): + return "%s" % name + return "%s_%s" % (prefix, name) + +def getPrefixPointer(name, typ, prefix): + ''' + Description: Make pointer name + Interface: None + History: 2019-06-17 + ''' + if typ != 'object' and typ != 'mapStringObject' and \ + not validBasicMapName(typ): + return "" + return '%s *' % makeBasicMapName(typ) if validBasicMapName(typ) \ + else "%s *" % getPrefixName(name, prefix) + +def judgeComplex(typ): + ''' + Description: Check compound object + Interface: None + History: 2019-06-17 + ''' + return typ in ('object', 'array', 'mapStringObject') + +def judgeDataType(typ): + ''' + Description: Check numeric type + Interface: None + History: 2019-06-17 + ''' + if (typ.startswith("int") or typ.startswith("uint")) and \ + "Pointer" not in typ: + return True + return typ in ("integer", "UID", "GID", "double") + +def judgeDataPointerType(typ): + ''' + Description: Check numeric pointer type + Interface: None + History: 2019-06-17 + ''' + if (typ.startswith("int") or typ.startswith("uint")) and "Pointer" in typ: + return True + return False + +def obtainDataPointerType(typ): + ''' + Description: Get numeric pointer type + Interface: None + History: 2019-06-17 + ''' + index = typ.find("Pointer") + return typ[0:index] if index != -1 else "" + +def obtainPointer(name, typ, prefix): + ''' + Description: Obtain pointer string + Interface: None + History: 2019-06-17 + ''' + ptr = getPrefixPointer(name, typ, prefix) + if ptr != "": + return ptr + + return "char *" if typ == "string" else \ + ("%s *" % typ if typ == "ArrayOfStrings" else "") + +class CombinationName(object): + ''' + Description: Store CombinationName information + Interface: None + History: 2019-06-17 + ''' + + def __init__(self, name, leaf=None): + self.name = name + self.leaf = leaf + + def __repr__(self): + return self.name + + def __str__(self): + return self.name + + def append(self, leaf): + ''' + Description: append name + Interface: None + History: 2019-06-17 + ''' + prefix_name = self.name + '_' if self.name != "" else "" + return CombinationName(prefix_name + leaf, leaf) + + +class Unite(object): + ''' + Description: Store Unite information + Interface: None + History: 2019-06-17 + ''' + def __init__(self, name, typ, children, subtyp=None, subtypobj=None, subtypname=None, \ + required=None): + self.typ = typ + self.children = children + self.subtyp = subtyp + self.subtypobj = subtypobj + self.subtypname = subtypname + self.required = required + self.name = convertToCStyle(name.name.replace('.', '_')) + self.origname = name.leaf or name.name + self.fixname = convertToCStyle(self.origname.replace('.', '_')) + + + + def __repr__(self): + if self.subtyp is not None: + return "name:(%s) type:(%s -> %s)" \ + % (self.name, self.typ, self.subtyp) + return "name:(%s) type:(%s)" % (self.name, self.typ) + + def __str__(self): + return self.__repr__(self) + + +class FilePath(object): + ''' + Description: Store filepath information + Interface: None + History: 2019-06-17 + ''' + def __init__(self, name): + self.name = os.path.realpath(name) + self.dirname = os.path.dirname(self.name) + self.basename = os.path.basename(self.name) + + def __repr__(self): + return "{name:(%s) dirname:(%s) basename:(%s)}" \ + % (self.name, self.dirname, self.basename) + + def __str__(self): + return self.__repr__(self) + + +class SchemaInfo(object): + ''' + Description: Store schema information + Interface: None + History: 2019-06-17 + ''' + + def __init__(self, name, header, source, prefix, filesdir, refs=None): + self.name = name + self.fileprefix = convertToCStyle( \ + name.basename.replace('.', '_').replace('-', '_')) + self.header = header + self.source = source + self.prefix = prefix + self.refs = refs + self.filesdir = os.path.realpath(filesdir) + + def __repr__(self): + return "{name:(%s) header:(%s) source:(%s) prefix:(%s)}" \ + % (self.name, self.header, self.source, self.prefix) + + def __str__(self): + return self.__repr__(self) diff --git a/src/json/schema/src/read_file.c b/src/json/schema/src/read_file.c new file mode 100644 index 0000000..e35de0c --- /dev/null +++ b/src/json/schema/src/read_file.c @@ -0,0 +1,151 @@ +/****************************************************************************** + * 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: tanyifeng + * Create: 2018-11-1 + * Description: provide file read functions + ********************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "securec.h" +#include "read_file.h" + +#ifndef O_CLOEXEC +#define O_CLOEXEC 02000000 +#endif + +#define JSON_MAX_SIZE (10LL * 1024LL * 1024LL) +#define FILE_MODE 0640 + +static int do_check_fread_args(const FILE *stream, const size_t *length) +{ + if (stream == NULL) { + return -1; + } + if (length == NULL) { + return -1; + } + + return 0; +} + +char *fread_file(FILE *stream, size_t *length) +{ + char *buf = NULL; + char *tmpbuf = NULL; + size_t off = 0; + + if (do_check_fread_args(stream, length) != 0) { + return NULL; + } + + while (1) { + size_t ret, newsize, sizejudge; + int pret; + errno_t rc = EOK; + sizejudge = (JSON_MAX_SIZE - BUFSIZ) - 1; + if (sizejudge < off) { + goto out; + } + newsize = off + BUFSIZ + 1; + + tmpbuf = (char *)calloc(1, newsize); + if (tmpbuf == NULL) { + goto out; + } + + if (buf != NULL) { + pret = memcpy_s(tmpbuf, newsize, buf, off); + if (pret) { + goto out; + } + + rc = memset_s(buf, off, 0, off); + if (rc != EOK) { + goto out; + } + + free(buf); + } + + buf = tmpbuf; + tmpbuf = NULL; + + ret = fread(buf + off, 1, BUFSIZ, stream); + if (ret == 0 && ferror(stream)) { + goto out; + } + if (ret < BUFSIZ || feof(stream)) { + *length = off + ret + 1; + buf[*length - 1] = '\0'; + return buf; + } + + off += BUFSIZ; + } +out: + free(buf); + free(tmpbuf); + return NULL; +} + +static int do_check_args(const char *path, const size_t *length) +{ + if (path == NULL || length == NULL) { + return -1; + } + if (strlen(path) > PATH_MAX) { + return -1; + } + return 0; +} + +char *read_file(const char *path, size_t *length) +{ + char *buf = NULL; + char rpath[PATH_MAX + 1] = { 0 }; + int fd = -1; + int tmperrno = -1; + FILE *fp = NULL; + + if (do_check_args(path, length) != 0) { + return NULL; + } + + if (realpath(path, rpath) == NULL) { + return NULL; + } + + fd = open(rpath, O_RDONLY | O_CLOEXEC, FILE_MODE); + if (fd < 0) { + return NULL; + } + + fp = fdopen(fd, "r"); + tmperrno = errno; + if (fp == NULL) { + (void)close(fd); + errno = tmperrno; + return NULL; + } + + buf = fread_file(fp, length); + (void)fclose(fp); + return buf; +} diff --git a/src/json/schema/src/read_file.h b/src/json/schema/src/read_file.h new file mode 100644 index 0000000..e3a04ff --- /dev/null +++ b/src/json/schema/src/read_file.h @@ -0,0 +1,25 @@ +/***************************************************************************** + * 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: tanyifeng + * Create: 2018-11-08 + * Description: provide container read file definition + ****************************************************************************/ +#ifndef __JSON_READ_FILE_H_ +#define __JSON_READ_FILE_H_ + +#include +#include + +char *fread_file(FILE *stream, size_t *length); + +char *read_file(const char *path, size_t *length); + +#endif diff --git a/src/json/schema/src/sources.py b/src/json/schema/src/sources.py new file mode 100644 index 0000000..e7b70a2 --- /dev/null +++ b/src/json/schema/src/sources.py @@ -0,0 +1,1013 @@ +# -*- coding: utf-8 -*- +""" +Copyright (C) Huawei Technologies., Ltd. 2017-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. +Description: generate json +Author: tanyifeng +Interface: None +History: 2018-04-25 created +2019-06-17 Code specification +""" +#!/usr/bin/python -Es +import helpers + + +def appendCCode(obj, c_file, prefix): + """ + Description: append c language code to file + Interface: None + History: 2019-06-17 + """ + parseJsonToC(obj, c_file, prefix) + makeCFree(obj, c_file, prefix) + obtainCJson(obj, c_file, prefix) + +def parseMapStringObject(obj, c_file, prefix, obj_typename): + """ + Description: generate c language for parse json map string object + Interface: None + History: 2019-06-17 + """ + child = obj.children[0] + if helpers.validBasicMapName(child.typ): + childname = helpers.makeBasicMapName(child.typ) + else: + if child.subtypname: + childname = child.subtypname + else: + childname = helpers.getPrefixName(child.name, prefix) + c_file.write(' if (YAJL_GET_OBJECT(tree) != NULL && YAJL_GET_OBJECT(tree)->len > 0) {\n') + c_file.write(' size_t i;\n') + c_file.write(' ret->len = YAJL_GET_OBJECT(tree)->len;\n') + c_file.write(' ret->keys = safe_malloc((YAJL_GET_OBJECT(tree)->len + 1) ' \ + '* sizeof(*ret->keys));\n') + c_file.write(' ret->%s = safe_malloc((YAJL_GET_OBJECT(tree)->len + 1) ' \ + '* sizeof(*ret->%s));\n' % (child.fixname, child.fixname)) + c_file.write(' for (i = 0; i < YAJL_GET_OBJECT(tree)->len; i++) {\n') + c_file.write(' const char *tmpkey = YAJL_GET_OBJECT(tree)->keys[i];\n') + c_file.write(' ret->keys[i] = safe_strdup(tmpkey ? tmpkey : "");\n') + c_file.write(' yajl_val val = YAJL_GET_OBJECT(tree)->values[i];\n') + c_file.write(' ret->%s[i] = make_%s(val, ctx, err);\n' \ + % (child.fixname, childname)) + c_file.write(' if (ret->%s[i] == NULL) {\n' % (child.fixname)) + c_file.write(" free_%s(ret);\n" % obj_typename) + c_file.write(" return NULL;\n") + c_file.write(' }\n') + c_file.write(' }\n') + c_file.write(' }\n') + +def parseObjectType(obj, c_file, prefix, obj_typename): + """ + Description: generate c language for parse object type + Interface: None + History: 2019-06-17 + """ + if obj.typ == 'string': + c_file.write(' {\n') + readValueGenerator(c_file, 2, 'get_val(tree, "%s", yajl_t_string)' % obj.origname, \ + "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) + c_file.write(' }\n') + elif helpers.judgeDataType(obj.typ): + c_file.write(' {\n') + readValueGenerator(c_file, 2, 'get_val(tree, "%s", yajl_t_number)' % obj.origname, \ + "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) + c_file.write(' }\n') + elif helpers.judgeDataPointerType(obj.typ): + c_file.write(' {\n') + readValueGenerator(c_file, 2, 'get_val(tree, "%s", yajl_t_number)' % obj.origname, \ + "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) + c_file.write(' }\n') + if obj.typ == 'boolean': + c_file.write(' {\n') + readValueGenerator(c_file, 2, 'get_val(tree, "%s", yajl_t_true)' % obj.origname, \ + "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) + c_file.write(' }\n') + if obj.typ == 'booleanPointer': + c_file.write(' {\n') + readValueGenerator(c_file, 2, 'get_val(tree, "%s", yajl_t_true)' % obj.origname, \ + "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) + c_file.write(' }\n') + elif obj.typ == 'object' or obj.typ == 'mapStringObject': + if obj.subtypname is not None: + typename = obj.subtypname + else: + typename = helpers.getPrefixName(obj.name, prefix) + c_file.write( + ' ret->%s = make_%s(get_val(tree, "%s", yajl_t_object), ctx, err);\n' \ + % (obj.fixname, typename, obj.origname)) + c_file.write(" if (ret->%s == NULL && *err != 0) {\n" % obj.fixname) + c_file.write(" free_%s(ret);\n" % obj_typename) + c_file.write(" return NULL;\n") + c_file.write(" }\n") + elif obj.typ == 'array' and (obj.subtypobj or obj.subtyp == 'object'): + if obj.subtypname: + typename = obj.subtypname + else: + typename = helpers.getNameSubstr(obj.name, prefix) + c_file.write(' {\n') + c_file.write(' yajl_val tmp = get_val(tree, "%s", yajl_t_array);\n' \ + % (obj.origname)) + c_file.write(' if (tmp != NULL && YAJL_GET_ARRAY(tmp) != NULL &&' \ + ' YAJL_GET_ARRAY(tmp)->len > 0) {\n') + c_file.write(' size_t i;\n') + c_file.write(' ret->%s_len = YAJL_GET_ARRAY(tmp)->len;\n' % (obj.fixname)) + c_file.write(' ret->%s = safe_malloc((YAJL_GET_ARRAY(tmp)->len + 1) ' \ + '* sizeof(*ret->%s));\n' % (obj.fixname, obj.fixname)) + c_file.write(' for (i = 0; i < YAJL_GET_ARRAY(tmp)->len; i++) {\n') + c_file.write(' yajl_val val = YAJL_GET_ARRAY(tmp)->values[i];\n') + c_file.write(' ret->%s[i] = make_%s(val, ctx, err);\n' \ + % (obj.fixname, typename)) + c_file.write(' if (ret->%s[i] == NULL) {\n' % (obj.fixname)) + c_file.write(" free_%s(ret);\n" % obj_typename) + c_file.write(" return NULL;\n") + c_file.write(' }\n') + c_file.write(' }\n') + c_file.write(' }\n') + c_file.write(' }\n') + elif obj.typ == 'array' and obj.subtyp == 'byte': + c_file.write(' {\n') + c_file.write(' yajl_val tmp = get_val(tree, "%s", yajl_t_string);\n' \ + % (obj.origname)) + c_file.write(' if (tmp != NULL) {\n') + c_file.write(' char *str = YAJL_GET_STRING(tmp);\n') + c_file.write(' ret->%s = (uint8_t *)safe_strdup(str ? str : "");\n' \ + % obj.fixname) + c_file.write(' ret->%s_len = str != NULL ? strlen(str) : 0;\n' \ + % obj.fixname) + c_file.write(' }\n') + c_file.write(' }\n') + elif obj.typ == 'array': + c_file.write(' {\n') + c_file.write(' yajl_val tmp = get_val(tree, "%s", yajl_t_array);\n' \ + % (obj.origname)) + c_file.write(' if (tmp != NULL && YAJL_GET_ARRAY(tmp) != NULL &&' \ + ' YAJL_GET_ARRAY(tmp)->len > 0) {\n') + c_file.write(' size_t i;\n') + c_file.write(' ret->%s_len = YAJL_GET_ARRAY(tmp)->len;\n' % (obj.fixname)) + c_file.write( + ' ret->%s = safe_malloc((YAJL_GET_ARRAY(tmp)->len + 1) *' \ + ' sizeof(*ret->%s));\n' % (obj.fixname, obj.fixname)) + c_file.write(' for (i = 0; i < YAJL_GET_ARRAY(tmp)->len; i++) {\n') + readValueGenerator(c_file, 4, 'YAJL_GET_ARRAY(tmp)->values[i]', \ + "ret->%s[i]" % obj.fixname, obj.subtyp, obj.origname, obj_typename) + c_file.write(' }\n') + c_file.write(' }\n') + c_file.write(' }\n') + elif helpers.validBasicMapName(obj.typ): + c_file.write(' {\n') + c_file.write(' yajl_val tmp = get_val(tree, "%s", yajl_t_object);\n' \ + % (obj.origname)) + c_file.write(' if (tmp != NULL) {\n') + c_file.write(' ret->%s = make_%s(tmp, ctx, err);\n' \ + % (obj.fixname, helpers.makeBasicMapName(obj.typ))) + c_file.write(' if (ret->%s == NULL) {\n' % (obj.fixname)) + c_file.write(' char *new_error = NULL;\n') + c_file.write(" if (asprintf(&new_error, \"Value error for key" \ + " '%s': %%s\", *err ? *err : \"null\") < 0) {\n" % (obj.origname)) + c_file.write(' new_error = safe_strdup(' \ + '"error allocating memory");\n') + c_file.write(' }\n') + c_file.write(' free(*err);\n') + c_file.write(' *err = new_error;\n') + c_file.write(' free_%s(ret);\n' % obj_typename) + c_file.write(' return NULL;\n') + c_file.write(' }\n') + c_file.write(' }\n') + c_file.write(' }\n') + +def parseObjectOrArrayObject(obj, c_file, prefix, obj_typename): + """ + Description: generate c language for parse object or array object + Interface: None + History: 2019-06-17 + """ + nodes = obj.children if obj.typ == 'object' else obj.subtypobj + + required_to_check = [] + for i in nodes or []: + if obj.required and i.origname in obj.required and \ + not helpers.judgeDataType(i.typ) and i.typ != 'boolean': + required_to_check.append(i) + parseObjectType(i, c_file, prefix, obj_typename) + + for i in required_to_check: + c_file.write(' if (ret->%s == NULL) {\n' % i.fixname) + c_file.write(' if (asprintf(err, "Required field \'%%s\' not present", ' \ + ' "%s") < 0)\n' % i.origname) + c_file.write(' *err = safe_strdup("error allocating memory");\n') + c_file.write(" free_%s(ret);\n" % obj_typename) + c_file.write(" return NULL;\n") + c_file.write(' }\n') + + if obj.typ == 'object' and obj.children is not None: + # O(n^2) complexity, but the objects should not really be big... + condition = " &&\n ".join( \ + ['strcmp(tree->u.object.keys[i], "%s")' % i.origname for i in obj.children]) + c_file.write(""" + if (tree->type == yajl_t_object && (ctx->options & OPT_PARSE_STRICT)) { + size_t i; + for (i = 0; i < tree->u.object.len; i++) + if (%s) { + if (ctx->stderr > 0) + (void)fprintf(ctx->stderr, "WARNING: unknown key found: %%s\\n", + tree->u.object.keys[i]); + } + } +""" % condition) + +def parseJsonToC(obj, c_file, prefix): + """ + Description: generate c language for parse json file + Interface: None + History: 2019-06-17 + """ + if not helpers.judgeComplex(obj.typ): + return + + if obj.typ == 'object' or obj.typ == 'mapStringObject': + if obj.subtypname: + return + obj_typename = typename = helpers.getPrefixName(obj.name, prefix) + + if obj.typ == 'array': + obj_typename = typename = helpers.getNameSubstr(obj.name, prefix) + objs = obj.subtypobj + if objs is None or obj.subtypname: + return + + c_file.write("%s *make_%s(yajl_val tree, const struct parser_context *ctx, "\ + "parser_error *err) {\n" % (typename, typename)) + c_file.write(" %s *ret = NULL;\n" % (typename)) + c_file.write(" *err = 0;\n") + c_file.write(" if (tree == NULL)\n") + c_file.write(" return ret;\n") + c_file.write(" ret = safe_malloc(sizeof(*ret));\n") + + if obj.typ == 'mapStringObject': + parseMapStringObject(obj, c_file, prefix, obj_typename) + + if obj.typ == 'object' or (obj.typ == 'array' and obj.subtypobj): + parseObjectOrArrayObject(obj, c_file, prefix, obj_typename) + + c_file.write(' return ret;\n') + c_file.write("}\n\n") + + +def obtainMapStringObject(obj, c_file, prefix): + """ + Description: c language generate map string object + Interface: None + History: 2019-06-17 + """ + child = obj.children[0] + if helpers.validBasicMapName(child.typ): + childname = helpers.makeBasicMapName(child.typ) + else: + if child.subtypname: + childname = child.subtypname + else: + childname = helpers.getPrefixName(child.name, prefix) + c_file.write(' size_t len = 0, i;\n') + c_file.write(" if (ptr != NULL)\n") + c_file.write(" len = ptr->len;\n") + c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.write(' yajl_gen_config(g, yajl_gen_beautify, 0);\n') + c_file.write(" stat = yajl_gen_map_open((yajl_gen)g);\n") + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' if (len ||(ptr != NULL && ptr->keys != NULL && ptr->%s != NULL)) {\n' \ + % child.fixname) + c_file.write(' for (i = 0; i < len; i++) {\n') + c_file.write(' char *str = ptr->keys[i] ? ptr->keys[i] : "";\n') + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)str, strlen(str));\n') + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' stat = gen_%s(g, ptr->%s[i], ctx, err);\n' \ + % (childname, child.fixname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' }\n') + c_file.write(' }\n') + c_file.write(" stat = yajl_gen_map_close((yajl_gen)g);\n") + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.write(' yajl_gen_config(g, yajl_gen_beautify, 1);\n') + +def obtainObjectOrArrayObject(obj, c_file, prefix): + """ + Description: c language generate object or array object + Interface: None + History: 2019-06-17 + """ + if obj.typ == 'string': + c_file.write(' if ((ctx->options & OPT_GEN_KAY_VALUE) ||' \ + ' (ptr != NULL && ptr->%s != NULL)) {\n' % obj.fixname) + c_file.write(' char *str = "";\n') + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (ptr != NULL && ptr->%s != NULL) {\n" % obj.fixname) + c_file.write(" str = ptr->%s;\n" % obj.fixname) + c_file.write(" }\n") + jsonValueGenerator(c_file, 2, "str", 'g', 'ctx', obj.typ) + c_file.write(" }\n") + + elif helpers.judgeDataType(obj.typ): + c_file.write(' if ((ctx->options & OPT_GEN_KAY_VALUE) ||' \ + ' (ptr != NULL && ptr->%s)) {\n' % obj.fixname) + if obj.typ == 'double': + numtyp = 'double' + elif obj.typ.startswith("uint") or obj.typ == 'GID' or obj.typ == 'UID': + numtyp = 'long long unsigned int' + else: + numtyp = 'long long int' + c_file.write(' %s num = 0;\n' % numtyp) + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (ptr != NULL && ptr->%s) {\n" % obj.fixname) + c_file.write(" num = (%s)ptr->%s;\n" % (numtyp, obj.fixname)) + c_file.write(" }\n") + jsonValueGenerator(c_file, 2, "num", 'g', 'ctx', obj.typ) + c_file.write(" }\n") + + elif helpers.judgeDataPointerType(obj.typ): + c_file.write(' if ((ptr != NULL && ptr->%s != NULL)) {\n' % obj.fixname) + numtyp = helpers.obtainDataPointerType(obj.typ) + if numtyp == "": + return + c_file.write(' %s num = 0;\n' % helpers.getMapCTypes(numtyp)) + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (ptr != NULL && ptr->%s != NULL) {\n" % obj.fixname) + c_file.write(" num = (%s)*(ptr->%s);\n" \ + % (helpers.getMapCTypes(numtyp), obj.fixname)) + c_file.write(" }\n") + jsonValueGenerator(c_file, 2, "num", 'g', 'ctx', numtyp) + c_file.write(" }\n") + + elif obj.typ == 'boolean': + c_file.write(' if ((ctx->options & OPT_GEN_KAY_VALUE) ||' \ + ' (ptr != NULL && ptr->%s)) {\n' % obj.fixname) + c_file.write(' bool b = false;\n') + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (ptr != NULL && ptr->%s) {\n" % obj.fixname) + c_file.write(" b = ptr->%s;\n" % obj.fixname) + c_file.write(" }\n") + jsonValueGenerator(c_file, 2, "b", 'g', 'ctx', obj.typ) + c_file.write(" }\n") + elif obj.typ == 'object' or obj.typ == 'mapStringObject': + if obj.subtypname: + typename = obj.subtypname + else: + typename = helpers.getPrefixName(obj.name, prefix) + c_file.write(' if ((ctx->options & OPT_GEN_KAY_VALUE) ||' \ + ' (ptr != NULL && ptr->%s != NULL)) {\n' % obj.fixname) + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' stat = gen_%s(g, ptr != NULL ? ptr->%s : NULL, ctx, err);\n' \ + % (typename, obj.fixname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" }\n") + + elif obj.typ == 'array' and (obj.subtypobj or obj.subtyp == 'object'): + if obj.subtypname: + typename = obj.subtypname + else: + typename = helpers.getNameSubstr(obj.name, prefix) + c_file.write(' if ((ctx->options & OPT_GEN_KAY_VALUE) || ' \ + '(ptr != NULL && ptr->%s != NULL)) {\n' % obj.fixname) + c_file.write(' size_t len = 0, i;\n') + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (ptr != NULL && ptr->%s != NULL) {\n" % obj.fixname) + c_file.write(" len = ptr->%s_len;\n" % obj.fixname) + c_file.write(" }\n") + c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.write(' yajl_gen_config(g, yajl_gen_beautify, 0);\n') + c_file.write(' stat = yajl_gen_array_open((yajl_gen)g);\n') + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' for (i = 0; i < len; i++) {\n') + c_file.write(' stat = gen_%s(g, ptr->%s[i], ctx, err);\n' \ + % (typename, obj.fixname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' }\n') + c_file.write(' stat = yajl_gen_array_close((yajl_gen)g);\n') + c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.write(' yajl_gen_config(g, yajl_gen_beautify, 1);\n') + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' }\n') + elif obj.typ == 'array' and obj.subtyp == 'byte': + c_file.write(' if ((ctx->options & OPT_GEN_KAY_VALUE) ||' \ + ' (ptr != NULL && ptr->%s != NULL && ptr->%s_len)) {\n' \ + % (obj.fixname, obj.fixname)) + c_file.write(' const char *str = "";\n') + c_file.write(' size_t len = 0;\n') + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (ptr != NULL && ptr->%s != NULL) {\n" % obj.fixname) + c_file.write(" str = (const char *)ptr->%s;\n" % obj.fixname) + c_file.write(" len = ptr->%s_len;\n" % obj.fixname) + c_file.write(" }\n") + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)str, len);\n') + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" }\n") + elif obj.typ == 'array': + c_file.write(' if ((ctx->options & OPT_GEN_KAY_VALUE) || ' \ + '(ptr != NULL && ptr->%s != NULL)) {\n' % obj.fixname) + c_file.write(' size_t len = 0, i;\n') + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (ptr != NULL && ptr->%s != NULL) {\n" % obj.fixname) + c_file.write(" len = ptr->%s_len;\n" % obj.fixname) + c_file.write(" }\n") + c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.write(' yajl_gen_config(g, yajl_gen_beautify, 0);\n') + c_file.write(' stat = yajl_gen_array_open((yajl_gen)g);\n') + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' for (i = 0; i < len; i++) {\n') + jsonValueGenerator(c_file, 3, "ptr->%s[i]" % obj.fixname, 'g', 'ctx', obj.subtyp) + c_file.write(' }\n') + c_file.write(' stat = yajl_gen_array_close((yajl_gen)g);\n') + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.write(' yajl_gen_config(g, yajl_gen_beautify, 1);\n') + c_file.write(' }\n') + + elif helpers.validBasicMapName(obj.typ): + c_file.write(' if ((ctx->options & OPT_GEN_KAY_VALUE) || ' \ + '(ptr != NULL && ptr->%s != NULL)) {\n' % obj.fixname) + c_file.write(' stat = yajl_gen_string((yajl_gen)g, (const unsigned char *)("%s"), strlen("%s"));\n' \ + % (obj.origname, obj.origname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(' stat = gen_%s(g, ptr ? ptr->%s : NULL, ctx, err);\n' \ + % (helpers.makeBasicMapName(obj.typ), obj.fixname)) + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + c_file.write(" }\n") + +def obtainCJson(obj, c_file, prefix): + """ + Description: c language generate json file + Interface: None + History: 2019-06-17 + """ + if not helpers.judgeComplex(obj.typ) or obj.subtypname: + return + if obj.typ == 'object' or obj.typ == 'mapStringObject': + obj_typename = typename = helpers.getPrefixName(obj.name, prefix) + elif obj.typ == 'array': + obj_typename = typename = helpers.getNameSubstr(obj.name, prefix) + objs = obj.subtypobj + if objs is None: + return + + c_file.write( + "yajl_gen_status gen_%s(yajl_gen g, const %s *ptr, const struct parser_context " \ + "*ctx, parser_error *err) {\n" % (typename, typename)) + c_file.write(" yajl_gen_status stat = yajl_gen_status_ok;\n") + c_file.write(" *err = 0;\n") + + if obj.typ == 'mapStringObject': + obtainMapStringObject(obj, c_file, prefix) + elif obj.typ == 'object' or (obj.typ == 'array' and obj.subtypobj): + nodes = obj.children if obj.typ == 'object' else obj.subtypobj + if nodes is None: + c_file.write(' if (!(ctx->options & OPT_GEN_SIMPLIFY))\n') + c_file.write(' yajl_gen_config(g, yajl_gen_beautify, 0);\n') + + c_file.write(" stat = yajl_gen_map_open((yajl_gen)g);\n") + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + + for i in nodes or []: + obtainObjectOrArrayObject(i, c_file, prefix) + + c_file.write(" stat = yajl_gen_map_close((yajl_gen)g);\n") + c_file.write(" if (yajl_gen_status_ok != stat)\n") + c_file.write(" GEN_SET_ERROR_AND_RETURN(stat, err);\n") + if nodes is None: + c_file.write(' if (!(ctx->options & OPT_GEN_SIMPLIFY))\n') + c_file.write(' yajl_gen_config(g, yajl_gen_beautify, 1);\n') + c_file.write(' return yajl_gen_status_ok;\n') + c_file.write("}\n\n") + + +def readValueGenerator(c_file, level, src, dest, typ, keyname, obj_typename): + """ + Description: read value generateor + Interface: None + History: 2019-06-17 + """ + if helpers.validBasicMapName(typ): + c_file.write('%syajl_val val = %s;\n' % (' ' * level, src)) + c_file.write('%sif (val != NULL) {\n' % (' ' * level)) + c_file.write('%s%s = make_%s(val, ctx, err);\n' \ + % (' ' * (level + 1), dest, helpers.makeBasicMapName(typ))) + c_file.write('%sif (%s == NULL) {\n' % (' ' * (level + 1), dest)) + c_file.write('%s char *new_error = NULL;\n' % (' ' * (level + 1))) + c_file.write("%s if (asprintf(&new_error, \"Value error for key" \ + " '%s': %%s\", *err ? *err : \"null\") < 0) {\n" \ + % (' ' * (level + 1), keyname)) + c_file.write('%s new_error = safe_strdup("error allocating memory");\n' \ + % (' ' * (level + 1))) + c_file.write('%s }\n' % (' ' * (level + 1))) + c_file.write('%s free(*err);\n' % (' ' * (level + 1))) + c_file.write('%s *err = new_error;\n' % (' ' * (level + 1))) + c_file.write('%s free_%s(ret);\n' % (' ' * (level + 1), obj_typename)) + c_file.write('%s return NULL;\n' % (' ' * (level + 1))) + c_file.write('%s}\n' % (' ' * (level + 1))) + c_file.write('%s}\n' % (' ' * (level))) + elif typ == 'string': + c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) + c_file.write('%sif (val != NULL) {\n' % (' ' * (level))) + c_file.write('%schar *str = YAJL_GET_STRING(val);\n' % (' ' * (level + 1))) + c_file.write('%s%s = safe_strdup(str ? str : "");\n' % (' ' * (level + 1), dest)) + c_file.write('%s}\n' % (' ' * level)) + elif helpers.judgeDataType(typ): + c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) + c_file.write('%sif (val != NULL) {\n' % (' ' * (level))) + if typ.startswith("uint") or \ + (typ.startswith("int") and typ != "integer") or typ == "double": + c_file.write('%sint invalid = common_safe_%s(YAJL_GET_NUMBER(val), &%s);\n' \ + % (' ' * (level + 1), typ, dest)) + elif typ == "integer": + c_file.write('%sint invalid = common_safe_int(YAJL_GET_NUMBER(val), (int *)&%s);\n' \ + % (' ' * (level + 1), dest)) + elif typ == "UID" or typ == "GID": + c_file.write('%sint invalid = common_safe_uint(YAJL_GET_NUMBER(val),' \ + ' (unsigned int *)&%s);\n' % (' ' * (level + 1), dest)) + c_file.write('%sif (invalid) {\n' % (' ' * (level + 1))) + c_file.write('%s if (asprintf(err, "Invalid value \'%%s\' with type \'%s\' ' + 'for key \'%s\': %%s", YAJL_GET_NUMBER(val), strerror(-invalid)) < 0)\n' \ + % (' ' * (level + 1), typ, keyname)) + c_file.write('%s *err = safe_strdup("error allocating memory");\n' \ + % (' ' * (level + 1))) + c_file.write('%s free_%s(ret);\n' % (' ' * (level + 1), obj_typename)) + c_file.write('%s return NULL;\n' % (' ' * (level + 1))) + c_file.write('%s}\n' % (' ' * (level + 1))) + c_file.write('%s}\n' % (' ' * (level))) + elif helpers.judgeDataPointerType(typ): + num_type = helpers.obtainDataPointerType(typ) + if num_type == "": + return + c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) + c_file.write('%sif (val != NULL) {\n' % (' ' * (level))) + c_file.write('%s%s = safe_malloc(sizeof(%s));\n' % + (' ' * (level + 1), dest, helpers.getMapCTypes(num_type))) + c_file.write('%sint invalid = common_safe_%s(YAJL_GET_NUMBER(val), %s);\n' \ + % (' ' * (level + 1), num_type, dest)) + c_file.write('%sif (invalid) {\n' % (' ' * (level + 1))) + c_file.write('%s if (asprintf(err, "Invalid value \'%%s\' with type \'%s\' ' \ + 'for key \'%s\': %%s", YAJL_GET_NUMBER(val), strerror(-invalid)) < 0)\n' \ + % (' ' * (level + 1), typ, keyname)) + c_file.write('%s *err = safe_strdup("error allocating memory");\n' \ + % (' ' * (level + 1))) + c_file.write('%s free_%s(ret);\n' % (' ' * (level + 1), obj_typename)) + c_file.write('%s return NULL;\n' % (' ' * (level + 1))) + c_file.write('%s}\n' % (' ' * (level + 1))) + c_file.write('%s}\n' % (' ' * (level))) + elif typ == 'boolean': + c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) + c_file.write('%sif (val != NULL)\n' % (' ' * (level))) + c_file.write('%s%s = YAJL_IS_TRUE(val);\n' % (' ' * (level + 1), dest)) + elif typ == 'booleanPointer': + c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) + c_file.write('%sif (val != NULL) {\n' % (' ' * (level))) + c_file.write('%s%s = safe_malloc(sizeof(bool));\n' % (' ' * (level + 1), dest)) + c_file.write('%s*(%s) = YAJL_IS_TRUE(val);\n' % (' ' * (level + 1), dest)) + c_file.write('%s} else {\n' % (' ' * (level))) + c_file.write('%sval = get_val(tree, "%s", yajl_t_false);\n' \ + % (' ' * (level + 1), keyname)) + c_file.write('%sif (val != NULL) {\n' % (' ' * (level + 1))) + c_file.write('%s%s = safe_malloc(sizeof(bool));\n' % (' ' * (level + 2), dest)) + c_file.write('%s*(%s) = YAJL_IS_TRUE(val);\n' % (' ' * (level + 2), dest)) + c_file.write('%s}\n' % (' ' * (level + 1))) + c_file.write('%s}\n' % (' ' * (level))) + + +def jsonValueGenerator(c_file, level, src, dst, ptx, typ): + """ + Description: json value generateor + Interface: None + History: 2019-06-17 + """ + if helpers.validBasicMapName(typ): + c_file.write('%sstat = gen_%s(%s, %s, %s, err);\n' \ + % (' ' * (level), helpers.makeBasicMapName(typ), dst, src, ptx)) + c_file.write("%sif (yajl_gen_status_ok != stat)\n" % (' ' * (level))) + c_file.write("%sGEN_SET_ERROR_AND_RETURN(stat, err);\n" % (' ' * (level + 1))) + elif typ == 'string': + c_file.write('%sstat = yajl_gen_string((yajl_gen)%s, (const unsigned char *)(%s), strlen(%s));\n' \ + % (' ' * (level), dst, src, src)) + c_file.write("%sif (yajl_gen_status_ok != stat)\n" % (' ' * (level))) + c_file.write("%sGEN_SET_ERROR_AND_RETURN(stat, err);\n" % (' ' * (level + 1))) + + elif helpers.judgeDataType(typ): + if typ == 'double': + c_file.write('%sstat = yajl_gen_double((yajl_gen)%s, %s);\n' % (' ' * (level), dst, src)) + elif typ.startswith("uint") or typ == 'GID' or typ == 'UID': + c_file.write('%sstat = map_uint(%s, %s);\n' % (' ' * (level), dst, src)) + else: + c_file.write('%sstat = map_int(%s, %s);\n' % (' ' * (level), dst, src)) + c_file.write("%sif (yajl_gen_status_ok != stat)\n" % (' ' * (level))) + c_file.write("%sGEN_SET_ERROR_AND_RETURN(stat, err);\n" % (' ' * (level + 1))) + + elif typ == 'boolean': + c_file.write('%sstat = yajl_gen_bool((yajl_gen)%s, (int)(%s));\n' % (' ' * (level), dst, src)) + c_file.write("%sif (yajl_gen_status_ok != stat)\n" % (' ' * (level))) + c_file.write("%sGEN_SET_ERROR_AND_RETURN(stat, err);\n" % (' ' * (level + 1))) + + +def makeCFree(obj, c_file, prefix): + """ + Description: generate c free function + Interface: None + History: 2019-06-17 + """ + if not helpers.judgeComplex(obj.typ) or obj.subtypname: + return + + typename = helpers.getPrefixName(obj.name, prefix) + + case = obj.typ + result = { + 'mapStringObject': lambda x: [], + 'object': lambda x: x.children, + 'array': lambda x: x.subtypobj + }[case](obj) + + objs = result + if obj.typ == 'array': + if objs is None: + return + else: + typename = helpers.getNameSubstr(obj.name, prefix) + + c_file.write("void free_%s(%s *ptr) {\n" % (typename, typename)) + c_file.write(" if (ptr == NULL)\n") + c_file.write(" return;\n") + if obj.typ == 'mapStringObject': + child = obj.children[0] + if helpers.validBasicMapName(child.typ): + childname = helpers.makeBasicMapName(child.typ) + else: + if child.subtypname: + childname = child.subtypname + else: + childname = helpers.getPrefixName(child.name, prefix) + c_file.write(" if (ptr->keys != NULL && ptr->%s != NULL) {\n" % child.fixname) + c_file.write(" size_t i;\n") + c_file.write(" for (i = 0; i < ptr->len; i++) {\n") + c_file.write(" free(ptr->keys[i]);\n") + c_file.write(" ptr->keys[i] = NULL;\n") + c_file.write(" free_%s(ptr->%s[i]);\n" % (childname, child.fixname)) + c_file.write(" ptr->%s[i] = NULL;\n" % (child.fixname)) + c_file.write(" }\n") + c_file.write(" free(ptr->keys);\n") + c_file.write(" ptr->keys = NULL;\n") + c_file.write(" free(ptr->%s);\n" % (child.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (child.fixname)) + c_file.write(" }\n") + + for i in objs or []: + if helpers.validBasicMapName(i.typ): + free_func = helpers.makeBasicMapName(i.typ) + c_file.write(" free_%s(ptr->%s);\n" % (free_func, i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + if i.typ == 'mapStringObject': + if i.subtypname: + free_func = i.subtypname + else: + free_func = helpers.getPrefixName(i.name, prefix) + c_file.write(" free_%s(ptr->%s);\n" % (free_func, i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + elif i.typ == 'array': + if helpers.validBasicMapName(i.subtyp): + free_func = helpers.makeBasicMapName(i.subtyp) + c_file.write(" if (ptr->%s != NULL) {\n" % i.fixname) + c_file.write(" size_t i;\n") + c_file.write(" for (i = 0; i < ptr->%s_len; i++) {\n" % i.fixname) + c_file.write(" if (ptr->%s[i] != NULL) {\n" % (i.fixname)) + c_file.write(" free_%s(ptr->%s[i]);\n" % (free_func, i.fixname)) + c_file.write(" ptr->%s[i] = NULL;\n" % (i.fixname)) + c_file.write(" }\n") + c_file.write(" }\n") + c_file.write(" free(ptr->%s);\n" % (i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + c_file.write(" }\n") + elif i.subtyp == 'string': + c_file.write(" if (ptr->%s != NULL) {\n" % i.fixname) + c_file.write(" size_t i;\n") + c_file.write(" for (i = 0; i < ptr->%s_len; i++) {\n" % i.fixname) + c_file.write(" if (ptr->%s[i] != NULL) {\n" % (i.fixname)) + c_file.write(" free(ptr->%s[i]);\n" % (i.fixname)) + c_file.write(" ptr->%s[i] = NULL;\n" % (i.fixname)) + c_file.write(" }\n") + c_file.write(" }\n") + c_file.write(" free(ptr->%s);\n" % (i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + c_file.write(" }\n") + elif not helpers.judgeComplex(i.subtyp): + c_file.write(" free(ptr->%s);\n" % (i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + elif i.subtyp == 'object' or i.subtypobj is not None: + if i.subtypname is not None: + free_func = i.subtypname + else: + free_func = helpers.getNameSubstr(i.name, prefix) + c_file.write(" if (ptr->%s != NULL) {\n" % i.fixname) + c_file.write(" size_t i;\n") + c_file.write(" for (i = 0; i < ptr->%s_len; i++)\n" % i.fixname) + c_file.write(" if (ptr->%s[i] != NULL) {\n" % (i.fixname)) + c_file.write(" free_%s(ptr->%s[i]);\n" % (free_func, i.fixname)) + c_file.write(" ptr->%s[i] = NULL;\n" % (i.fixname)) + c_file.write(" }\n") + c_file.write(" free(ptr->%s);\n" % i.fixname) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + c_file.write(" }\n") + + c_typ = helpers.obtainPointer(i.name, i.subtypobj, prefix) + if c_typ == "": + continue + if i.subobj is not None: + c_typ = c_typ + "_element" + c_file.write(" free_%s(ptr->%s);\n" % (c_typ, i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + else: # not array + typename = helpers.getPrefixName(i.name, prefix) + if i.typ == 'string': + c_file.write(" free(ptr->%s);\n" % (i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + elif i.typ == 'booleanPointer': + c_file.write(" free(ptr->%s);\n" % (i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + elif helpers.judgeDataPointerType(i.typ): + c_file.write(" free(ptr->%s);\n" % (i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + elif i.typ == 'object': + if i.subtypname is not None: + typename = i.subtypname + c_file.write(" if (ptr->%s != NULL) {\n" % (i.fixname)) + c_file.write(" free_%s(ptr->%s);\n" % (typename, i.fixname)) + c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + c_file.write(" }\n") + c_file.write(" free(ptr);\n") + c_file.write("}\n\n") + + +def sourceReflection(structs, schema_info, c_file, root_typ): + """ + Description: reflect code + Interface: None + History: 2019-06-17 + """ + c_file.write("// Generated from %s. Do not edit!\n" % (schema_info.name.basename)) + c_file.write("#ifndef _GNU_SOURCE\n") + c_file.write("#define _GNU_SOURCE\n") + c_file.write("#endif\n") + c_file.write('#include \n') + c_file.write('#include \n') + c_file.write('#include "securec.h"\n') + c_file.write('#include "%s"\n\n' % schema_info.header.basename) + + for i in structs: + appendCCode(i, c_file, schema_info.prefix) + + obtainCEpilogue(c_file, schema_info.prefix, root_typ) + + +def obtainCEpilogue(c_file, prefix, typ): + """ + Description: generate c language epilogue + Interface: None + History: 2019-06-17 + """ + if typ != 'array' and typ != 'object': + return + + if typ == 'array': + c_file.write("""\n +%s_element **make_%s(yajl_val tree, const struct parser_context *ctx, parser_error *err, size_t *len) { + %s_element **ptr = NULL; + size_t i, alen; + if (tree == NULL || err == NULL || !len || YAJL_GET_ARRAY(tree) == NULL) + return NULL; + *err = 0; + alen = YAJL_GET_ARRAY(tree)->len; + if (alen == 0) + return NULL; + ptr = safe_malloc((alen + 1) * sizeof(%s_element *)); + for (i = 0; i < alen; i++) { + yajl_val val = YAJL_GET_ARRAY(tree)->values[i]; + ptr[i] = make_%s_element(val, ctx, err); + if (ptr[i] == NULL) { + free_%s(ptr, alen); + return NULL; + } + } + *len = alen; + return ptr; +} +""" % (prefix, prefix, prefix, prefix, prefix, prefix)) + c_file.write("""\n +void free_%s(%s_element **ptr, size_t len) { + size_t i; + + if (ptr == NULL || len == 0) + return; + + for (i = 0; i < len; i++) { + if (ptr[i] != NULL) { + free_%s_element(ptr[i]); + ptr[i] = NULL; + } + } + free(ptr); +} +""" % (prefix, prefix, prefix)) + c_file.write("""\n +yajl_gen_status gen_%s(yajl_gen g, const %s_element **ptr, size_t len, const struct parser_context *ctx, + parser_error *err) { + yajl_gen_status stat; + size_t i; + *err = 0; + stat = yajl_gen_array_open((yajl_gen)g); + if (yajl_gen_status_ok != stat) + GEN_SET_ERROR_AND_RETURN(stat, err); + for (i = 0; i < len; i++) { + stat = gen_%s_element(g, ptr[i], ctx, err); + if (yajl_gen_status_ok != stat) + GEN_SET_ERROR_AND_RETURN(stat, err); + } + stat = yajl_gen_array_close((yajl_gen)g); + if (yajl_gen_status_ok != stat) + GEN_SET_ERROR_AND_RETURN(stat, err); + return yajl_gen_status_ok; +} +""" % (prefix, prefix, prefix)) + + c_file.write(""" +%s%s*%s_parse_file(const char *filename, const struct parser_context *ctx, parser_error *err%s) { + %s%s*ptr = NULL;""" % (prefix, ' ' if typ == 'object' else '_element *', \ + prefix, '' if typ == 'object' else ', size_t *len', \ + prefix, ' ' if typ == 'object' else '_element *')) + + c_file.write(""" + size_t filesize; + char *content = NULL; + + if (filename == NULL || err == NULL) + return NULL; + + *err = NULL; + content = read_file(filename, &filesize); + if (content == NULL) { + if (asprintf(err, "cannot read the file: %%s", filename) < 0) + *err = safe_strdup("error allocating memory"); + return NULL; + } + ptr = %s_parse_data(content, ctx, err%s); + free(content); + return ptr; +} +""" % (prefix, '' if typ == 'object' else ', len')) + + c_file.write(""" +%s%s*%s_parse_file_stream(FILE *stream, const struct parser_context *ctx, parser_error *err%s) { + %s%s*ptr = NULL;""" % (prefix, ' ' if typ == 'object' else '_element *', \ + prefix, '' if typ == 'object' else ', size_t *len', \ + prefix, ' ' if typ == 'object' else '_element *')) + + c_file.write(""" + size_t filesize; + char *content = NULL ; + + if (stream == NULL || err == NULL) + return NULL; + + *err = NULL; + content = fread_file(stream, &filesize); + if (content == NULL) { + *err = safe_strdup("cannot read the file"); + return NULL; + } + ptr = %s_parse_data(content, ctx, err%s); + free(content); + return ptr; +} +""" % (prefix, '' if typ == 'object' else ', len')) + + c_file.write(""" +%s%s*%s_parse_data(const char *jsondata, const struct parser_context *ctx, parser_error *err%s) { + %s%s*ptr = NULL;""" % (prefix, ' ' if typ == 'object' else '_element *', \ + prefix, '' if typ == 'object' else ', size_t *len', \ + prefix, ' ' if typ == 'object' else '_element *')) + + c_file.write(""" + yajl_val tree; + char errbuf[1024]; + struct parser_context tmp_ctx = { 0 }; + + if (jsondata == NULL || err == NULL) + return NULL; + + *err = NULL; + if (ctx == NULL) { + ctx = (const struct parser_context *)(&tmp_ctx); + } + tree = yajl_tree_parse(jsondata, errbuf, sizeof(errbuf)); + if (tree == NULL) { + if (asprintf(err, "cannot parse the data: %%s", errbuf) < 0) + *err = safe_strdup("error allocating memory"); + return NULL; + } + ptr = make_%s(tree, ctx, err%s); + yajl_tree_free(tree); + return ptr; +} +""" % (prefix, '' if typ == 'object' else ', len')) + + c_file.write("char *%s_generate_json(const %s%s*ptr%s, const struct parser_context *ctx," \ + " parser_error *err) {" % (prefix, prefix, \ + ' ' if typ == 'object' else '_element *', \ + '' if typ == 'object' else ', size_t len')) + + c_file.write(""" + yajl_gen g = NULL; + struct parser_context tmp_ctx = { 0 }; + const unsigned char *gen_buf = NULL; + char *json_buf = NULL; + size_t gen_len = 0; + + if (ptr == NULL || err == NULL) + return NULL; + + *err = NULL; + if (ctx == NULL) { + ctx = (const struct parser_context *)(&tmp_ctx); + } + + if (!json_gen_init(&g, ctx)) { + *err = safe_strdup("Json_gen init failed"); + goto out; + } + if (yajl_gen_status_ok != gen_%s(g, ptr%s, ctx, err)) { + if (*err == NULL) + *err = safe_strdup("Failed to generate json"); + goto free_out; + } + yajl_gen_get_buf(g, &gen_buf, &gen_len); + if (gen_buf == NULL) { + *err = safe_strdup("Error to get generated json"); + goto free_out; + } + + json_buf = safe_malloc(gen_len + 1); + if (memcpy_s(json_buf, gen_len + 1, gen_buf, gen_len) != EOK) { + *err = safe_strdup("Error to memcpy json"); + free(json_buf); + json_buf = NULL; + goto free_out; + } + json_buf[gen_len] = '\\0'; + +free_out: + yajl_gen_clear(g); + yajl_gen_free(g); +out: + return json_buf; +} + +""" % (prefix, '' if typ == 'object' else ', len')) diff --git a/src/lcr_list.h b/src/lcr_list.h new file mode 100644 index 0000000..aa60df6 --- /dev/null +++ b/src/lcr_list.h @@ -0,0 +1,138 @@ +/****************************************************************************** + * 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 lcr list definition + ******************************************************************************/ +#ifndef __LCR_LIST_H +#define __LCR_LIST_H + + +struct lcr_list { + void *elem; + struct lcr_list *next; + struct lcr_list *prev; +}; + +/* Iterate through an lcr list.*/ +#define lcr_list_for_each(__iterator, __list) \ + for ((__iterator) = (__list)->next; \ + (__iterator) != (__list); \ + (__iterator) = (__iterator)->next) + +/* + * Iterate safely through an lcr list + */ +#define lcr_list_for_each_safe(__iterator, __list, __next) \ + for ((__iterator) = (__list)->next, (__next) = (__iterator)->next; \ + (__iterator) != (__list); \ + (__iterator) = (__next), (__next) = (__next)->next) + +/* Initialize list. */ +static inline void lcr_list_init(struct lcr_list *list) +{ + list->elem = NULL; + list->next = list->prev = list; +} + +/* Add an element to a list. See lcr_list_add() and lcr_list_add_tail() for an + * idiom. */ +static inline void lcr_list_add_elem(struct lcr_list *list, void *elem) +{ + list->elem = elem; +} + +/* Retrieve first element of list. */ +static inline void *lcr_list_first_elem(struct lcr_list *list) +{ + return list->next->elem; +} + +/* Retrieve last element of list. */ +static inline void *lcr_list_last_elem(struct lcr_list *list) +{ + return list->prev->elem; +} + +/* Determine if list is empty. */ +static inline int lcr_list_empty(const struct lcr_list *list) +{ + return list == list->next; +} + +/* Workhorse to be called from lcr_list_add() and lcr_list_add_tail(). */ +static inline void __lcr_list_add(struct lcr_list *new, struct lcr_list *prev, struct lcr_list *next) +{ + next->prev = new; + new->next = next; + new->prev = prev; + prev->next = new; +} + +/* + * Idiom to add an element to the beginning of an lcr list + */ +static inline void lcr_list_add(struct lcr_list *head, struct lcr_list *list) +{ + __lcr_list_add(list, head, head->next); +} + +/* + * Idiom to add an element to the end of an lcr list + */ +static inline void lcr_list_add_tail( + struct lcr_list *head, struct lcr_list *list) +{ + __lcr_list_add(list, head->prev, head); +} + +/* Idiom to merge two lcr list */ +static inline void lcr_list_merge( + struct lcr_list *list1, struct lcr_list *list2) +{ + struct lcr_list *list1_tail, *list2_tail; + list1_tail = list1->prev; + list2_tail = list2->prev; + // to merge two list, we need: + // 1. update list1's prev (next is not needed) to list2's tail + list1->prev = list2_tail; + // 2. update list1's tail's next to list2 + list1_tail->next = list2; + // 3. update list2's prev to list1's tail + list2->prev = list1_tail; + // 4. update list2's tail's next to list1 + list2_tail->next = list1; +} + +/* Idiom to free an lcr list */ +static inline void lcr_list_del(struct lcr_list *list) +{ + struct lcr_list *next, *prev; + + next = list->next; + prev = list->prev; + next->prev = prev; + prev->next = next; +} + +/* Return length of the list. */ +static inline size_t lcr_list_len(struct lcr_list *list) +{ + size_t i = 0; + struct lcr_list *iter; + lcr_list_for_each(iter, list) { + i++; + } + + return i; +} + +#endif diff --git a/src/lcrcontainer.c b/src/lcrcontainer.c new file mode 100644 index 0000000..53ef2c2 --- /dev/null +++ b/src/lcrcontainer.c @@ -0,0 +1,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 +#include +#include +#include +#include + +#include + +#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; +} diff --git a/src/lcrcontainer.h b/src/lcrcontainer.h new file mode 100644 index 0000000..ac78205 --- /dev/null +++ b/src/lcrcontainer.h @@ -0,0 +1,313 @@ +/****************************************************************************** + * 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 + */ + +#ifndef __LCR_CONTAINER_H +#define __LCR_CONTAINER_H +#include +#include +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/* define console log config */ + +struct lcr_console_config { + char *log_path; + unsigned int log_rotate; + char *log_file_size; +}; + +/* + * Store lcr container info + */ +struct lcr_container_info { + /* Name of container.*/ + char *name; + /* State of container.*/ + char *state; + /* Interface of container.*/ + char *interface; + char *ipv4; + char *ipv6; + pid_t init; + double ram; + double swap; + bool running; +}; + +struct blkio_stats { + uint64_t read; + uint64_t write; + uint64_t total; +}; + +/* + * Store lcr container state + */ +struct lcr_container_state { + /* Name of container */ + char *name; + /* State of container */ + char *state; + /* The process ID of the init container*/ + pid_t init; + /* Current pids */ + uint64_t pids_current; + /* CPU usage*/ + uint64_t cpu_use_nanos; + uint64_t cpu_use_user; + uint64_t cpu_use_sys; + /* BlkIO usage*/ + struct blkio_stats io_service_bytes; + struct blkio_stats io_serviced; + /* Memory usage */ + uint64_t mem_used; + uint64_t mem_limit; + /* Kernel Memory usage */ + uint64_t kmem_used; + uint64_t kmem_limit; +}; + +typedef enum { + lcr_msg_state, + lcr_msg_priority, + lcr_msg_exit_code, +} lcr_msg_type_t; + +struct lcr_msg { + lcr_msg_type_t type; + char name[NAME_MAX + 1]; + int value; + int pid; +}; + +struct lcr_cgroup_resources { + uint64_t blkio_weight; + uint64_t cpu_shares; + uint64_t cpu_period; + uint64_t cpu_quota; + char *cpuset_cpus; + char *cpuset_mems; + uint64_t memory_limit; + uint64_t memory_swap; + uint64_t memory_reservation; + uint64_t kernel_memory_limit; +}; + +/* + * Get one container info for a given lcrpath. + * return struct of container info, or NULL on error. + */ +struct lcr_container_info *lcr_container_info_get(const char *name, const char *lcrpath); + +/* + * Free lcr_container_info returned lcr_container_info_get + */ +void lcr_container_info_free(struct lcr_container_info *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); + +/* + * 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); + +/* + * Create a container + * param name : container name + * param lcrpath: container path + * param rootfs : the path of rootfs used for the container + * param dist : the distribution for generate specification + * NOTE: if the dist is `none`, it will not create default spec + * param oci_config_data : json string of oci config data + */ +bool lcr_create(const char *name, const char *lcrpath, const char *rootfs, + const char *dist, const void *oci_config_data); + +/* + * Start a container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param logpath : log file path. + * param loglevel : log level. + * param pidfile : container pidfile path, set to NULL if you don't need. + * param daemonize : daemonize the container. + * console_fifos[] : path of the console fifos,[0]:input, [1]:output.used internal by lcrd + * console_logpath :path of console log file, + * set to NULL if want to use the default configure(base on the config file) + set to PATH(for example "/home/XX/XX.log"), LXC will save the console to this file + * share_ns : array of container's name or pid which want to share namespace with them + * start_timeout : seconds for waiting on a container to start before it is killed + * container_pidfile : container's pidfile + * param argv : array of arguments to pass to init. + * uid : user to run container + * gid : user in which group + * additional_gids : Add additional groups to join + */ +struct lcr_start_request { + const char *name; + const char *lcrpath; + + const char *logpath; + const char *loglevel; + + bool daemonize; + bool tty; + bool open_stdin; + const char *pidfile; + const char **console_fifos; + const char *console_logpath; + uint32_t start_timeout; + const char *container_pidfile; + const char *exit_fifo; + + uid_t uid; + gid_t gid; + gid_t *additional_gids; + size_t additional_gids_len; + + const char **share_ns; +}; +bool lcr_start(const struct lcr_start_request *request); + +/* + * Stop a container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param signal : signal to send to the container. + */ +bool lcr_kill(const char *name, const char *lcrpath, uint32_t signal); + +/* + * Delete a container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param force : force to delete container + */ +bool lcr_delete(const char *name, const char *lcrpath); + +/* + * Execute process inside a container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param argc : the size of the argv array + * param argv : array of arguments to be execute inside container + * param pid : ID of process running inside container + */ +bool lcr_exec(const char *name, const char *lcrpath, int argc, char * const * argv, pid_t *pid); + + +bool lcr_clean(const char *name, const char *lcrpath, const char *logpath, const char *loglevel, pid_t pid); + +/* + * Get state of the container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param lcs : returned contaiener state + */ +bool lcr_state(const char *name, const char *lcrpath, struct lcr_container_state *lcs); + +/* + * Pause a container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + */ +bool lcr_pause(const char *name, const char *lcrpath); + +/* + * Resume a container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + */ +bool lcr_resume(const char *name, const char *lcrpath); + +/* + * Free lcr_container_state returned by lcr_state + */ +void lcr_container_state_free(struct lcr_container_state *lcs); + +/* + * console function + * param name : name of container + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param in_fifo : fifo names of input FIFO + * param out_fifo : fifo names of output FIFO + * Returns false if the console FIFOs add failed, true if success + */ +bool lcr_console(const char *name, const char *lcrpath, const char *in_fifo, const char *out_fifo, + const char *err_fifo); + +/* + * get container console configs + * param name : name of container + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param config : use to store container console configs, cannot be NULL + */ +bool lcr_get_console_config(const char *name, const char *lcrpath, struct lcr_console_config *config); + +void lcr_free_console_config(struct lcr_console_config *config); + +int lcr_log_init(const char *name, const char *file, const char *priority, + const char *prefix, int quiet, const char *lcrpath); + +/* + * Execute process inside a container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param stdinfd : stdinfd + * param stdoutfd : stdoutfd + * param stderrfd : stderrfd + * param escape : prefix for escape command, for example if escape equals 1 means the escape + * prefix is , you can type to exit the console. + * -1 means never exit console by hand. + * param argc : the size of the argv array + * param argv : array of arguments to be execute inside container(terminated with "NULL") + * param env : array of environment variables to be set inside container(terminated with "NULL") + * param timeout : Timeout in seconds for attach container + * param pid : ID of process running inside container + * param exit_code : exit_code of process running inside container + */ +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 *pid, int *exit_code); + +bool lcr_update(const char *name, const char *lcrpath, const struct lcr_cgroup_resources *cr); + +const char *lcr_get_errmsg(); + +void lcr_free_errmsg(); + +bool lcr_get_container_pids(const char *name, const char *lcrpath, pid_t **pids, size_t *pids_len); + +bool translate_spec(const struct lxc_container *c, const char *oci_json_data, const char *container_rootfs); + +#ifdef __cplusplus +} +#endif + +#endif /* __LCR_CONTAINER_H */ diff --git a/src/lcrcontainer_execute.c b/src/lcrcontainer_execute.c new file mode 100644 index 0000000..04c32b6 --- /dev/null +++ b/src/lcrcontainer_execute.c @@ -0,0 +1,1092 @@ +/****************************************************************************** + * 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 +#include +#include +#include +#include +#include + +#include "constants.h" +#include "lcrcontainer_execute.h" +#include "utils.h" +#include "log.h" +#include "error.h" +#include "securec.h" +#include "oci_runtime_spec.h" + +// Cgroup Item Definition +#define CGROUP_BLKIO_WEIGHT "blkio.weight" +#define CGROUP_CPU_SHARES "cpu.shares" +#define CGROUP_CPU_PERIOD "cpu.cfs_period_us" +#define CGROUP_CPU_QUOTA "cpu.cfs_quota_us" +#define CGROUP_CPUSET_CPUS "cpuset.cpus" +#define CGROUP_CPUSET_MEMS "cpuset.mems" +#define CGROUP_MEMORY_LIMIT "memory.limit_in_bytes" +#define CGROUP_MEMORY_SWAP "memory.memsw.limit_in_bytes" +#define CGROUP_MEMORY_RESERVATION "memory.soft_limit_in_bytes" + +#define REPORT_SET_CGROUP_ERROR(item, value) \ + do { \ + SYSERROR("Error updating cgroup %s to %s", (item), (value)); \ + lcr_set_error_message(LCR_ERR_RUNTIME, "Error updating cgroup %s to %s: %s", (item), (value), \ + strerror(errno)); \ + } while (0) + +static inline void add_array_elem(char **array, size_t total, size_t *pos, const char *elem) +{ + if (*pos + 1 >= total - 1) { + return; + } + array[*pos] = util_strdup_s(elem); + *pos += 1; +} + +static inline void add_array_kv(char **array, size_t total, size_t *pos, const char *k, const char *v) +{ + if (k == NULL || v == NULL) { + return; + } + add_array_elem(array, total, pos, k); + add_array_elem(array, total, pos, v); +} + +static int make_sure_linux(oci_runtime_spec *oci_spec) +{ + if (oci_spec->linux == NULL) { + oci_spec->linux = util_common_calloc_s(sizeof(oci_runtime_config_linux)); + if (oci_spec->linux == NULL) { + return -1; + } + } + return 0; +} + +static int make_sure_linux_resources(oci_runtime_spec *oci_spec) +{ + int ret = 0; + + ret = make_sure_linux(oci_spec); + if (ret < 0) { + return -1; + } + + if (oci_spec->linux->resources == NULL) { + oci_spec->linux->resources = util_common_calloc_s(sizeof(oci_runtime_config_linux_resources)); + if (oci_spec->linux->resources == NULL) { + return -1; + } + } + return 0; +} + +static int make_sure_linux_resources_blkio(oci_runtime_spec *oci_spec) +{ + int ret = 0; + + ret = make_sure_linux_resources(oci_spec); + if (ret < 0) { + return -1; + } + + if (oci_spec->linux->resources->block_io == NULL) { + oci_spec->linux->resources->block_io = + util_common_calloc_s(sizeof(oci_runtime_config_linux_resources_block_io)); + if (oci_spec->linux->resources->block_io == NULL) { + return -1; + } + } + return 0; +} + +static int make_sure_linux_resources_cpu(oci_runtime_spec *oci_spec) +{ + int ret = 0; + + ret = make_sure_linux_resources(oci_spec); + if (ret < 0) { + return -1; + } + + if (oci_spec->linux->resources->cpu == NULL) { + oci_spec->linux->resources->cpu = util_common_calloc_s(sizeof(oci_runtime_config_linux_resources_cpu)); + if (oci_spec->linux->resources->cpu == NULL) { + return -1; + } + } + return 0; +} + +static int make_sure_linux_resources_mem(oci_runtime_spec *oci_spec) +{ + int ret = 0; + + ret = make_sure_linux_resources(oci_spec); + if (ret < 0) { + return -1; + } + + if (oci_spec->linux->resources->memory == NULL) { + oci_spec->linux->resources->memory = util_common_calloc_s(sizeof(oci_runtime_config_linux_resources_memory)); + if (oci_spec->linux->resources->memory == NULL) { + return -1; + } + } + return 0; +} + +static bool update_resources_cpuset_mems(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + bool ret = false; + + if (cr->cpuset_mems != NULL && strcmp(cr->cpuset_mems, "") != 0) { + if (!c->set_cgroup_item(c, CGROUP_CPUSET_MEMS, cr->cpuset_mems)) { + REPORT_SET_CGROUP_ERROR(CGROUP_CPUSET_MEMS, cr->cpuset_mems); + goto err_out; + } + } + ret = true; +err_out: + return ret; +} + +static bool update_resources_cpuset(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + bool ret = false; + if (cr->cpuset_cpus != NULL && strcmp(cr->cpuset_cpus, "") != 0) { + if (!c->set_cgroup_item(c, CGROUP_CPUSET_CPUS, cr->cpuset_cpus)) { + REPORT_SET_CGROUP_ERROR(CGROUP_CPUSET_CPUS, cr->cpuset_cpus); + goto err_out; + } + } + + ret = true; +err_out: + return ret; +} + +static int update_resources_cpu_shares(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + int ret = 0; + char numstr[128] = { 0 }; /* max buffer */ + + if (cr->cpu_shares != 0) { + if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_shares)) < 0) { + ret = -1; + goto out; + } + + if (!c->set_cgroup_item(c, CGROUP_CPU_SHARES, numstr)) { + REPORT_SET_CGROUP_ERROR(CGROUP_CPU_SHARES, numstr); + ret = -1; + goto out; + } + } + +out: + return ret; +} + +static int update_resources_cpu_period(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + int ret = 0; + char numstr[128] = { 0 }; /* max buffer */ + + if (cr->cpu_period != 0) { + if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_period)) < 0) { + ret = -1; + goto out; + } + + if (!c->set_cgroup_item(c, CGROUP_CPU_PERIOD, numstr)) { + REPORT_SET_CGROUP_ERROR(CGROUP_CPU_PERIOD, numstr); + ret = -1; + goto out; + } + } + +out: + return ret; +} + +static int update_resources_cpu_quota(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + int ret = 0; + char numstr[128] = { 0 }; /* max buffer */ + + if (cr->cpu_quota != 0) { + if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_quota)) < 0) { + ret = -1; + goto out; + } + + if (!c->set_cgroup_item(c, CGROUP_CPU_QUOTA, numstr)) { + REPORT_SET_CGROUP_ERROR(CGROUP_CPU_QUOTA, numstr); + ret = -1; + goto out; + } + } + +out: + return ret; +} + +static bool update_resources_cpu(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + bool ret = false; + + if (update_resources_cpu_shares(c, cr) != 0) { + goto err_out; + } + + if (update_resources_cpu_period(c, cr) != 0) { + goto err_out; + } + + if (update_resources_cpu_quota(c, cr) != 0) { + goto err_out; + } + + if (!update_resources_cpuset(c, cr)) { + goto err_out; + } + + if (!update_resources_cpuset_mems(c, cr)) { + goto err_out; + } + + ret = true; +err_out: + return ret; +} + +static int update_resources_memory_limit(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + int ret = 0; + char numstr[128] = { 0 }; /* max buffer */ + + if (cr->memory_limit != 0) { + if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_limit)) < 0) { + ret = -1; + goto out; + } + + if (!c->set_cgroup_item(c, CGROUP_MEMORY_LIMIT, numstr)) { + REPORT_SET_CGROUP_ERROR(CGROUP_MEMORY_LIMIT, numstr); + ret = -1; + goto out; + } + } + +out: + return ret; +} + +static int update_resources_memory_swap(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + int ret = 0; + char numstr[128] = { 0 }; /* max buffer */ + + if (cr->memory_swap != 0) { + if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_swap)) < 0) { + ret = -1; + goto out; + } + + if (!c->set_cgroup_item(c, CGROUP_MEMORY_SWAP, numstr)) { + REPORT_SET_CGROUP_ERROR(CGROUP_MEMORY_SWAP, numstr); + ret = -1; + goto out; + } + } + +out: + return ret; +} + +static int update_resources_memory_reservation(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + int ret = 0; + char numstr[128] = { 0 }; /* max buffer */ + + if (cr->memory_reservation != 0) { + if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_reservation)) < 0) { + ret = -1; + goto out; + } + + if (!c->set_cgroup_item(c, CGROUP_MEMORY_RESERVATION, numstr)) { + REPORT_SET_CGROUP_ERROR(CGROUP_MEMORY_RESERVATION, numstr); + ret = -1; + goto out; + } + } + +out: + return ret; +} + +static bool update_resources_mem(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + bool ret = false; + + if (update_resources_memory_limit(c, cr) != 0) { + goto err_out; + } + + if (update_resources_memory_swap(c, cr) != 0) { + goto err_out; + } + + if (update_resources_memory_reservation(c, cr) != 0) { + goto err_out; + } + + ret = true; +err_out: + return ret; +} + +static int update_resources_blkio_weight(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + int ret = 0; + char numstr[128] = { 0 }; /* max buffer */ + + if (cr->blkio_weight != 0) { + if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->blkio_weight)) < 0) { + ret = -1; + goto out; + } + + if (!c->set_cgroup_item(c, CGROUP_BLKIO_WEIGHT, numstr)) { + REPORT_SET_CGROUP_ERROR(CGROUP_BLKIO_WEIGHT, numstr); + ret = -1; + goto out; + } + } + +out: + return ret; +} + +static bool update_resources(struct lxc_container *c, const struct lcr_cgroup_resources *cr) +{ + bool ret = false; + + if (c == NULL || cr == NULL) { + return false; + } + + if (update_resources_blkio_weight(c, cr) != 0) { + goto err_out; + } + + if (!update_resources_cpu(c, cr)) { + goto err_out; + } + if (!update_resources_mem(c, cr)) { + goto err_out; + } + + ret = true; +err_out: + return ret; +} + +static bool save_container_to_disk(const struct lxc_container *c, const oci_runtime_spec *container) +{ + bool bret = true; + char *json_container = NULL; + struct parser_context ctr = { 0 }; + parser_error err = NULL; + + if (c == NULL || container == NULL) { + return false; + } + + ctr.options = OPT_PARSE_STRICT; + ctr.stderr = stderr; + + json_container = oci_runtime_spec_generate_json(container, &ctr, &err); + if (json_container == NULL) { + ERROR("Can not generate json: %s", err); + bret = false; + goto err_out; + } + if (!translate_spec(c, json_container, NULL)) { + bret = false; + goto err_out; + } +err_out: + free(json_container); + free(err); + return bret; +} + +static int merge_cgroup_resources_cpu_shares(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->cpu_shares != 0) { + if (make_sure_linux_resources_cpu(c) != 0) { + return -1; + } + c->linux->resources->cpu->shares = cr->cpu_shares; + } + + return 0; +} + +static int merge_cgroup_resources_cpu_period(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->cpu_period != 0) { + if (make_sure_linux_resources_cpu(c) != 0) { + return -1; + } + c->linux->resources->cpu->period = cr->cpu_period; + } + + return 0; +} + +static int merge_cgroup_resources_cpu_quota(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->cpu_quota != 0) { + if (make_sure_linux_resources_cpu(c) != 0) { + return -1; + } + c->linux->resources->cpu->quota = (int64_t)(cr->cpu_quota); + } + + return 0; +} + +static int merge_cgroup_resources_cpuset_cpus(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->cpuset_cpus != NULL && strcmp(cr->cpuset_cpus, "") != 0) { + if (make_sure_linux_resources_cpu(c) != 0) { + return -1; + } + free(c->linux->resources->cpu->cpus); + c->linux->resources->cpu->cpus = util_strdup_s(cr->cpuset_cpus); + } + + return 0; +} + +static int merge_cgroup_resources_cpuset_mems(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->cpuset_mems != NULL && strcmp(cr->cpuset_mems, "") != 0) { + if (make_sure_linux_resources_cpu(c) != 0) { + return -1; + } + free(c->linux->resources->cpu->mems); + c->linux->resources->cpu->mems = util_strdup_s(cr->cpuset_mems); + } + + return 0; +} + +static bool merge_cgroup_resources_cpu(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + bool ret = true; + + if (merge_cgroup_resources_cpu_shares(c, cr) != 0) { + ret = false; + goto out; + } + + if (merge_cgroup_resources_cpu_period(c, cr) != 0) { + ret = false; + goto out; + } + + if (merge_cgroup_resources_cpu_quota(c, cr) != 0) { + ret = false; + goto out; + } + + if (merge_cgroup_resources_cpuset_cpus(c, cr) != 0) { + ret = false; + goto out; + } + + if (merge_cgroup_resources_cpuset_mems(c, cr) != 0) { + ret = false; + goto out; + } + +out: + return ret; +} + +static int merge_cgroup_resources_mem_limit(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->memory_limit != 0) { + if (make_sure_linux_resources_mem(c) != 0) { + return -1; + } + c->linux->resources->memory->limit = (int64_t)(cr->memory_limit); + } + + return 0; +} + +static int merge_cgroup_resources_mem_reservation(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->memory_reservation != 0) { + if (make_sure_linux_resources_mem(c) != 0) { + return -1; + } + c->linux->resources->memory->reservation = (int64_t)(cr->memory_reservation); + } + + return 0; +} + +static int merge_cgroup_resources_mem_swap(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->memory_swap != 0) { + if (make_sure_linux_resources_mem(c) != 0) { + return -1; + } + c->linux->resources->memory->swap = (int64_t)(cr->memory_swap); + } + + return 0; +} + +static int merge_cgroup_resources_kernel_memory_limit(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->kernel_memory_limit != 0) { + if (make_sure_linux_resources_mem(c) != 0) { + return -1; + } + c->linux->resources->memory->kernel = (int64_t)(cr->kernel_memory_limit); + } + + return 0; +} + +static bool merge_cgroup_resources_mem(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + bool ret = true; + + if (merge_cgroup_resources_mem_limit(c, cr) != 0) { + ret = false; + goto out; + } + + if (merge_cgroup_resources_mem_reservation(c, cr) != 0) { + ret = false; + goto out; + } + + if (merge_cgroup_resources_mem_swap(c, cr) != 0) { + ret = false; + goto out; + } + + if (merge_cgroup_resources_kernel_memory_limit(c, cr) != 0) { + ret = false; + goto out; + } + +out: + return ret; +} + +static int merge_cgroup_resources_blkio_weight(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + if (cr->blkio_weight != 0) { + if (make_sure_linux_resources_blkio(c) != 0) { + return -1; + } + c->linux->resources->block_io->weight = (int)(cr->blkio_weight); + } + + return 0; +} + +static bool merge_cgroup_resources(oci_runtime_spec *c, const struct lcr_cgroup_resources *cr) +{ + bool ret = true; + + if (c == NULL || cr == NULL) { + return false; + } + + if (merge_cgroup_resources_blkio_weight(c, cr) != 0) { + ret = false; + goto out; + } + + ret = merge_cgroup_resources_cpu(c, cr); + if (!ret) { + goto out; + } + + ret = merge_cgroup_resources_mem(c, cr); + if (!ret) { + goto out; + } + +out: + return ret; +} + +bool do_update(struct lxc_container *c, const char *name, const char *lcrpath, const struct lcr_cgroup_resources *cr) +{ + int rc = 0; + bool bret = false; + bool restore = false; + char *config_json_file = NULL; + oci_runtime_spec *container = NULL; + oci_runtime_spec *backupcontainer = NULL; + struct parser_context ctr = { OPT_PARSE_STRICT, stderr }; + parser_error err = NULL; + + rc = asprintf(&config_json_file, "%s/%s/%s", lcrpath, name, OCICONFIGFILE); + if (rc < 0) { + SYSERROR("Failed to allocated memory"); + return false; + } + + backupcontainer = oci_runtime_spec_parse_file(config_json_file, &ctr, &err); + if (backupcontainer == NULL) { + ERROR("Can not parse %s: %s", OCICONFIGFILE, err); + goto out_free; + } + container = oci_runtime_spec_parse_file(config_json_file, &ctr, &err); + if (container == NULL) { + ERROR("Can not parse %s: %s", OCICONFIGFILE, err); + goto out_free; + } + + if (!merge_cgroup_resources(container, cr)) { + ERROR("Failed to merge cgroup resources"); + goto out_free; + } + + if (!save_container_to_disk(c, container)) { + ERROR("Failed to save config to disk"); + restore = true; + goto restore; + } + + // If container is not running, update config file is enough, + // resources will be updated when the container is started again. + // If container is running (including paused), we need to update configs + // to the real world. + if (c->is_running(c)) { + if (!update_resources(c, cr) && c->is_running(c)) { + ERROR("Filed to update cgroup resources"); + restore = true; + goto restore; + } + } + + bret = true; + +restore: + if (restore && !save_container_to_disk(c, backupcontainer)) { + ERROR("Failed to restore"); + bret = false; + } + +out_free: + free_oci_runtime_spec(backupcontainer); + free_oci_runtime_spec(container); + free(config_json_file); + free(err); + if (bret) { + clear_error_message(&g_lcr_error); + } + return bret; +} + +static inline bool is_blk_stat_read(const char *value) +{ + return strcmp(value, "Read") == 0; +} + +static inline bool is_blk_stat_write(const char *value) +{ + return strcmp(value, "Write") == 0; +} + +static inline bool is_blk_stat_total(const char *value) +{ + return strcmp(value, "Total") == 0; +} + +static void stat_get_blk_stats(struct lxc_container *c, const char *item, struct blkio_stats *stats) +{ + char buf[BUFSIZE] = { 0 }; + int i = 0; + size_t len = 0; + char **lines = NULL; + char **cols = NULL; + errno_t nret = 0; + + len = (size_t)c->get_cgroup_item(c, item, buf, sizeof(buf)); + if (len == 0 || len >= sizeof(buf)) { + DEBUG("unable to read cgroup item %s", item); + return; + } + + lines = lcr_string_split_and_trim(buf, '\n'); + if (lines == NULL) { + return; + } + + nret = memset_s(stats, sizeof(struct blkio_stats), 0, sizeof(struct blkio_stats)); + if (nret != EOK) { + goto err_out; + } + + for (i = 0; lines[i]; i++) { + cols = lcr_string_split_and_trim(lines[i], ' '); + if (cols == NULL) { + goto err_out; + } + if (is_blk_stat_read(cols[1])) { + stats->read += strtoull(cols[2], NULL, 0); + } else if (is_blk_stat_write(cols[1])) { + stats->write += strtoull(cols[2], NULL, 0); + } + if (is_blk_stat_total(cols[0])) { + stats->total = strtoull(cols[1], NULL, 0); + } + + lcr_free_array((void **)cols); + } +err_out: + lcr_free_array((void **)lines); + return; +} + +static uint64_t stat_match_get_ull(struct lxc_container *c, const char *item, const char *match, int column) +{ + char buf[BUFSIZE] = { 0 }; + int i = 0; + int j = 0; + int len = 0; + uint64_t val = 0; + char **lines = NULL; + char **cols = NULL; + size_t matchlen = 0; + + len = c->get_cgroup_item(c, item, buf, sizeof(buf)); + if (len <= 0) { + DEBUG("unable to read cgroup item %s", item); + goto err_out; + } + + lines = lcr_string_split_and_trim(buf, '\n'); + if (lines == NULL) { + goto err_out; + } + + matchlen = strlen(match); + for (i = 0; lines[i]; i++) { + if (strncmp(lines[i], match, matchlen) != 0) { + continue; + } + + cols = lcr_string_split_and_trim(lines[i], ' '); + if (cols == NULL) { + goto err1; + } + for (j = 0; cols[j]; j++) { + if (j == column) { + val = strtoull(cols[j], NULL, 0); + break; + } + } + lcr_free_array((void **)cols); + break; + } +err1: + lcr_free_array((void **)lines); +err_out: + return val; +} + +static uint64_t stat_get_ull(struct lxc_container *c, const char *item) +{ + char buf[80] = { 0 }; + int len = 0; + uint64_t val = 0; + + len = c->get_cgroup_item(c, item, buf, sizeof(buf)); + if (len <= 0) { + DEBUG("unable to read cgroup item %s", item); + return 0; + } + + val = strtoull(buf, NULL, 0); + return val; +} + +void do_lcr_state(struct lxc_container *c, struct lcr_container_state *lcs) +{ + const char *state = NULL; + + clear_error_message(&g_lcr_error); + if (memset_s(lcs, sizeof(struct lcr_container_state), 0x00, sizeof(struct lcr_container_state)) != EOK) { + ERROR("Can not set memory"); + } + + lcs->name = util_strdup_s(c->name); + + state = c->state(c); + lcs->state = state ? util_strdup_s(state) : util_strdup_s("-"); + + if (c->is_running(c)) { + lcs->init = c->init_pid(c); + } else { + lcs->init = -1; + } + + lcs->cpu_use_nanos = stat_get_ull(c, "cpuacct.usage"); + lcs->pids_current = stat_get_ull(c, "pids.current"); + + lcs->cpu_use_user = stat_match_get_ull(c, "cpuacct.stat", "user", 1); + lcs->cpu_use_sys = stat_match_get_ull(c, "cpuacct.stat", "system", 1); + + // Try to read CFQ stats available on all CFQ enabled kernels first + stat_get_blk_stats(c, "blkio.io_serviced_recursive", &lcs->io_serviced); + if (lcs->io_serviced.read == 0 && lcs->io_serviced.write == 0 && lcs->io_serviced.total == 0) { + stat_get_blk_stats(c, "blkio.throttle.io_service_bytes", &lcs->io_service_bytes); + stat_get_blk_stats(c, "blkio.throttle.io_serviced", &lcs->io_serviced); + } else { + stat_get_blk_stats(c, "blkio.io_service_bytes_recursive", &lcs->io_service_bytes); + } + + lcs->mem_used = stat_get_ull(c, "memory.usage_in_bytes"); + lcs->mem_limit = stat_get_ull(c, "memory.limit_in_bytes"); + lcs->kmem_used = stat_get_ull(c, "memory.kmem.usage_in_bytes"); + lcs->kmem_limit = stat_get_ull(c, "memory.kmem.limit_in_bytes"); +} + +#define ExitSignalOffset 128 + +static void execute_lxc_attach(const char *name, const char *path, const char *logpath, const char *loglevel, + const char *console_fifos[], const char * const argv[], const char * const env[], + int64_t timeout) +{ + // should check the size of params when add new params. + char **params = NULL; + size_t i = 0; + size_t args_len = PARAM_NUM; + const char * const *tmp = NULL; + + if (util_check_inherited(true, -1) != 0) { + COMMAND_ERROR("Close inherited fds failed"); + exit(EXIT_FAILURE); + } + + for (tmp = env; tmp != NULL && *tmp != NULL; tmp++) { + args_len++; + } + for (tmp = argv; tmp != NULL && *tmp != NULL; tmp++) { + args_len++; + } + + if (args_len > (SIZE_MAX / sizeof(char *))) { + exit(EXIT_FAILURE); + } + + params = util_common_calloc_s(args_len * sizeof(char *)); + if (params == NULL) { + COMMAND_ERROR("Out of memory"); + exit(EXIT_FAILURE); + } + add_array_elem(params, args_len, &i, "lxc-attach"); + add_array_elem(params, args_len, &i, "-n"); + add_array_elem(params, args_len, &i, name); + add_array_elem(params, args_len, &i, "-P"); + add_array_elem(params, args_len, &i, path); + add_array_elem(params, args_len, &i, "--clear-env"); + add_array_kv(params, args_len, &i, "--logfile", logpath); + add_array_kv(params, args_len, &i, "-l", loglevel); + add_array_kv(params, args_len, &i, "--in-fifo", console_fifos[0]); + add_array_kv(params, args_len, &i, "--out-fifo", console_fifos[1]); + for (tmp = env; tmp && *tmp; tmp++) { + add_array_elem(params, args_len, &i, "-v"); + add_array_elem(params, args_len, &i, *tmp); + } + + if (timeout != 0) { + char timeout_str[LCR_NUMSTRLEN64] = { 0 }; + add_array_elem(params, args_len, &i, "--timeout"); + if (sprintf_s(timeout_str, LCR_NUMSTRLEN64, "%lld", (long long)timeout) < 0) { + COMMAND_ERROR("Invaild attach timeout value :%lld", (long long)timeout); + free(params); + exit(EXIT_FAILURE); + } + add_array_elem(params, args_len, &i, timeout_str); + } + + add_array_elem(params, args_len, &i, "--"); + for (tmp = argv; tmp && *tmp; tmp++) { + add_array_elem(params, args_len, &i, *tmp); + } + + execvp("lxc-attach", params); + + COMMAND_ERROR("Failed to exec lxc-attach: %s", strerror(errno)); + free(params); + exit(EXIT_FAILURE); +} + +static int do_attach_get_exit_code(int status) +{ + int exit_code = 0; + + if (WIFEXITED(status)) { + exit_code = WEXITSTATUS(status); + } else { + exit_code = -1; + } + + if (WIFSIGNALED(status)) { + int signal; + signal = WTERMSIG(status); + exit_code = ExitSignalOffset + signal; + } + return exit_code; +} + +bool do_attach(const char *name, const char *path, const char *logpath, const char *loglevel, + const char *console_fifos[], const char * const argv[], const char * const env[], int64_t timeout, + pid_t *exec_pid, int *exit_code) +{ + bool ret = false; + pid_t pid = 0; + ssize_t size_read = 0; + char buffer[BUFSIZ] = { 0 }; + int pipefd[2] = { -1, -1 }; + int status = 0; + + if (pipe(pipefd) != 0) { + ERROR("Failed to create pipe\n"); + return false; + } + + pid = fork(); + if (pid == (pid_t) - 1) { + ERROR("Failed to fork()\n"); + close(pipefd[0]); + close(pipefd[1]); + goto out; + } + + if (pid == (pid_t)0) { + if (util_null_stdfds() < 0) { + COMMAND_ERROR("Failed to close fds"); + exit(EXIT_FAILURE); + } + setsid(); + + // child process, dup2 pipefd[1] to stderr + close(pipefd[0]); + dup2(pipefd[1], 2); + + execute_lxc_attach(name, path, logpath, loglevel, console_fifos, argv, env, timeout); + } + + close(pipefd[1]); + + status = wait_for_pid_status(pid); + if (status < 0) { + ERROR("Failed to wait lxc-attach"); + goto close_out; + } + + *exit_code = do_attach_get_exit_code(status); + + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { + size_read = read(pipefd[0], buffer, BUFSIZ); + /* if we read errmsg means the runtime failed to exec */ + if (size_read > 0) { + ERROR("Runtime error: %s", buffer); + lcr_set_error_message(LCR_ERR_RUNTIME, "runtime error: %s", buffer); + goto close_out; + } + } + ret = true; + +close_out: + close(pipefd[0]); +out: + return ret; +} + +void execute_lxc_start(const char *name, const char *path, const char *logpath, const char *loglevel, + bool daemonize, bool tty, bool open_stdin, const char *pidfile, + const char *console_fifos[], const char *console_logpath, const char *share_ns[], + uint32_t start_timeout, const char *container_pidfile, const char *exit_fifo) +{ + // should check the size of params when add new params. + char *params[PARAM_NUM] = { NULL }; + size_t i = 0; + + if (util_check_inherited(true, -1) != 0) { + COMMAND_ERROR("Close inherited fds failed"); + } + + add_array_elem(params, PARAM_NUM, &i, "lxc-start"); + add_array_elem(params, PARAM_NUM, &i, "-n"); + add_array_elem(params, PARAM_NUM, &i, name); + add_array_elem(params, PARAM_NUM, &i, "-P"); + add_array_elem(params, PARAM_NUM, &i, path); + add_array_elem(params, PARAM_NUM, &i, "--quiet"); + add_array_kv(params, PARAM_NUM, &i, "--logfile", logpath); + add_array_kv(params, PARAM_NUM, &i, "-l", loglevel); + add_array_kv(params, PARAM_NUM, &i, "--in-fifo", console_fifos[0]); + add_array_kv(params, PARAM_NUM, &i, "--out-fifo", console_fifos[1]); + add_array_kv(params, PARAM_NUM, &i, "--err-fifo", console_fifos[2]); + if (!tty) { + add_array_elem(params, PARAM_NUM, &i, "--disable-pty"); + } + if (open_stdin) { + add_array_elem(params, PARAM_NUM, &i, "--open-stdin"); + } + add_array_elem(params, PARAM_NUM, &i, daemonize ? "-d" : "-F"); + add_array_kv(params, PARAM_NUM, &i, "-p", pidfile); + add_array_kv(params, PARAM_NUM, &i, "-L", console_logpath); + add_array_kv(params, PARAM_NUM, &i, "--container-pidfile", container_pidfile); + add_array_kv(params, PARAM_NUM, &i, "--exit-fifo", exit_fifo); + + if (start_timeout != 0) { + char start_timeout_str[LCR_NUMSTRLEN64] = { 0 }; + add_array_elem(params, PARAM_NUM, &i, "--start-timeout"); + if (sprintf_s(start_timeout_str, LCR_NUMSTRLEN64, "%u", start_timeout) < 0) { + COMMAND_ERROR("Invaild start timeout value: %u", start_timeout); + exit(EXIT_FAILURE); + } + add_array_elem(params, PARAM_NUM, &i, start_timeout_str); + } + + execvp("lxc-start", params); + + COMMAND_ERROR("Failed to exec lxc-start\n"); + exit(EXIT_FAILURE); +} diff --git a/src/lcrcontainer_execute.h b/src/lcrcontainer_execute.h new file mode 100644 index 0000000..dccb244 --- /dev/null +++ b/src/lcrcontainer_execute.h @@ -0,0 +1,41 @@ +/****************************************************************************** + * 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 + ******************************************************************************/ +#ifndef __LCR_CONTAINER_EXECUTE_H +#define __LCR_CONTAINER_EXECUTE_H + +#include "lcrcontainer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +bool do_update(struct lxc_container *c, const char *name, const char *lcrpath, const struct lcr_cgroup_resources *cr); + +void do_lcr_state(struct lxc_container *c, struct lcr_container_state *lcs); + +bool do_attach(const char *name, const char *path, const char *logpath, const char *loglevel, + const char *console_fifos[], const char * const argv[], const char * const env[], int64_t timeout, + pid_t *exec_pid, int *exit_code); + +void execute_lxc_start(const char *name, const char *path, const char *logpath, const char *loglevel, + bool daemonize, bool tty, bool open_stdin, const char *pidfile, + const char *console_fifos[], const char *console_logpath, const char *share_ns[], + uint32_t start_timeout, const char *container_pidfile, const char *exit_fifo); + +#ifdef __cplusplus +} +#endif + +#endif /* __LCR_CONTAINER_EXECUTE_H */ diff --git a/src/lcrcontainer_extend.c b/src/lcrcontainer_extend.c new file mode 100644 index 0000000..892c66a --- /dev/null +++ b/src/lcrcontainer_extend.c @@ -0,0 +1,1097 @@ +/****************************************************************************** + * 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 +#include +#include +#include +#include + +#include + +#include "constants.h" +#include "error.h" +#include "lcrcontainer.h" +#include "lcrcontainer_extend.h" +#include "utils.h" +#include "log.h" +#include "conf.h" + +#define COMMON_CONFIG_ITEMS \ + { .name = "lxc.tty.dir", .value = "lxc" /* Setup the LXC devices in /dev/lxc */ }, \ + { .name = "lxc.pty.max", .value = "1024" /* Allow for 1024 pseudo terminals */ }, \ + { .name = "lxc.tty.max", .value = "4" /* Setup 4 tty devices */ }, /* Drop some harmful capabilities */ \ + { .name = "lxc.cap.drop", .value = "mac_admin mac_override sys_time sys_module sys_rawio" }, \ + { .name = "lxc.cgroup.devices.deny", \ + .value = "a" /* CGroup whitelist */ }, /* Allow any mknod (but not reading/writing the node */ \ + { .name = "lxc.cgroup.devices.allow", \ + .value = "c *:* m" }, /* Allow any mknod (but not reading/writing the node */ \ + { .name = "lxc.cgroup.devices.allow", .value = "b *:* m" }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 1:3 rwm" /* Allow /dev/null */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 1:5 rwm" /* Allow /dev/zero */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 1:7 rwm" /* Allow /dev/full */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 5:0 rwm" /* Allow /dev/tty */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 5:1 rwm" /* Allow /dev/console */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 5:2 rwm" /* Allow /dev/ptmx */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 1:8 rwm" /* Allow /dev/random */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 1:9 rwm" /* Allow /dev/urandom */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 136:* rwm" /* Allow /dev/pts */ }, \ + { .name = "lxc.cgroup.devices.allow", .value = "c 10:229 rwm" /* Allow fuse */ }, \ + { .name = "lxc.mount.auto", .value = "cgroup:mixed proc:mixed sys:mixed" /* default mounts */ }, \ + { .name = "lxc.mount.entry", \ + .value = "/sys/fs/fuse/connections sys/fs/fuse/connections non bind,optional 0 0" }, \ + { NULL, NULL } + +static const lcr_config_item_t g_app_config_items[] = { + /* Container specific configuration */ + { + .name = "lxc.arch", + .value = "x86_64", + }, + { + .name = "lxc.uts.name", + .value = "app", + }, + /* Extra mount entries */ + { + .name = "lxc.mount.entry", + .value = "/dev dev none ro,bind 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "/lib lib none ro,bind 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "/bin bin none ro,bind 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "/usr usr none ro,bind 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "/sbin sbin none ro,bind 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "tmpfs var/run tmpfs mode=0644 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "proc proc proc nodev,noexec,nosuid 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "sysfs sys sysfs ro,bind 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "/lib64 lib64 none ro,bind 0 0", + }, + /* Include common config */ + COMMON_CONFIG_ITEMS +}; + +static const lcr_config_item_t g_ubuntu_config_items[] = { + /* Container specific configuration */ + { + .name = "lxc.arch", + .value = "x86_64" + }, + { + .name = "lxc.uts.name", + .value = "ubuntu", + }, + /* Extra mount entries */ + { + .name = "lxc.mount.entry", + .value = "/sys/kernel/debug sys/kernel/debug none bind,optional 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "/sys/kernel/security sys/kernel/security none bind,optional 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "/sys/fs/pstore sys/fs/pstore none bind,optional 0 0", + }, + { + .name = "lxc.mount.entry", + .value = "mqueue dev/mqueue mqueue rw,realtime,create=dir,optional 0 0", + }, + /* Extra cgroup device access */ + { + .name = "lxc.cgroup.devices.allow", + .value = "c 254:0 rm", + }, /* rtc */ + { + .name = "lxc.cgroup.devices.allow", + .value = "c 10:200 rwm", + }, /* tun */ + { + .name = "lxc.cgroup.devices.allow", + .value = "c 10:228 rwm", + }, /* hpet */ + { + .name = "lxc.cgroup.devices.allow", + .value = "c 10:232 rwm", + }, /* kvm */ + /* Include common config */ + COMMON_CONFIG_ITEMS +}; + +static const char g_common_seccomp[] = "\ +2\n\ +blacklist\n\ +reject_force_umount # comment this to allow umount -f; not recommended\n\ +[all]\n\ +kexec_load errno 1\n\ +open_by_handle_at errno 1\n\ +init_module errno 1\n\ +finit_module errno 1\n\ +delete_module errno 1\n\ +"; + +static struct lxc_container *lcr_new_container(const char *name, const char *path) +{ + struct lxc_container *c = lxc_container_new(name, path); + if (c == NULL) { + ERROR("Failed to delete container."); + return NULL; + } + + if (!c->is_defined(c)) { + ERROR("No such container"); + lcr_set_error_message(LCR_ERR_RUNTIME, "No such container:%s or the configuration files has been corrupted", + name); + goto out_put; + } + + if (!c->may_control(c)) { + ERROR("Insufficent privileges to control"); + goto out_put; + } + return c; +out_put: + lxc_container_put(c); + return NULL; +} + +static int realloc_annotations(oci_runtime_spec *oci_spec, size_t new_lens) +{ + size_t new_size = 0; + unsigned long old_size = 0; + char **fkey = NULL; + char **fval = NULL; + int nret = 0; + + if (new_lens < 1) { + return 0; + } + + if (!oci_spec->annotations) { + oci_spec->annotations = util_common_calloc_s(sizeof(json_map_string_string)); + if (!oci_spec->annotations) { + ERROR("Out of memory"); + nret = -1; + goto out; + } + } + + if (new_lens > SIZE_MAX / sizeof(char *) - oci_spec->annotations->len) { + ERROR("Too many annotations!"); + nret = -1; + goto out; + } + new_size = (oci_spec->annotations->len + new_lens) * sizeof(char *); + old_size = oci_spec->annotations->len * sizeof(char *); + nret = mem_realloc((void **)&fkey, new_size, oci_spec->annotations->keys, old_size); + if (nret) { + ERROR("Failed to realloc memory for files limit variables"); + nret = -1; + goto out; + } + oci_spec->annotations->keys = fkey; + + nret = mem_realloc((void **)&fval, new_size, oci_spec->annotations->values, old_size); + if (nret) { + ERROR("Failed to realloc memory for files limit variables"); + nret = -1; + goto out; + } + oci_spec->annotations->values = fval; + oci_spec->annotations->len += new_lens; + +out: + return nret; +} + +static int make_annotations(oci_runtime_spec *container, const struct lxc_container *c, int fpos) +{ + int ret = -1; + int nret; + char default_path[PATH_MAX] = { 0 }; + char *realpath = NULL; + json_map_string_string *anno = container->annotations; + + if (fpos == -1) { + if (realloc_annotations(container, 1)) { + ERROR("Realloc annotations failed"); + goto out; + } + fpos = (int)(anno->len - 1); + anno->keys[fpos] = util_strdup_s("log.console.file"); + anno->values[fpos] = NULL; + } + + if (!anno->values[fpos]) { + nret = sprintf_s(default_path, PATH_MAX, "%s/%s/%s", c->config_path, c->name, "console.log"); + if (nret < 0) { + ERROR("create default path: %s failed", default_path); + goto out; + } + if (anno->values[fpos]) { + free(anno->values[fpos]); + } + anno->values[fpos] = util_strdup_s(default_path); + } + if (strcmp("none", anno->values[fpos]) == 0) { + DEBUG("Disable console log."); + ret = 0; + goto out; + } + if (util_ensure_path(&realpath, anno->values[fpos])) { + ERROR("Invalid log path: %s, error: %s.", anno->values[fpos], strerror(errno)); + goto out; + } + ret = 0; +out: + free(realpath); + return ret; +} + +static inline bool is_annotations_key_console_file(const char *key) +{ + return strcmp(key, "log.console.file") == 0; +} + +static int check_annotations(oci_runtime_spec *container, const struct lxc_container *c) +{ + size_t i; + int fpos = -1; + bool ret = false; + + if (c == NULL) { + return 0; + } + + if (!container->annotations) { + SAFE_MALLOC(container->annotations, sizeof(json_map_string_string), ret); + if (!ret) { + goto out; + } + } else { + for (i = 0; i < container->annotations->len; i++) { + if (is_annotations_key_console_file(container->annotations->keys[i])) { + fpos = (int)i; + } + } + } + + if (make_annotations(container, c, fpos)) { + goto out; + } + ret = true; +out: + return ret ? 0 : -1; +} + +static int trans_rootfs_linux(struct lcr_list *lcr_conf, const char *container_rootfs, oci_runtime_spec *container, + char **seccomp) +{ + int ret = -1; + struct lcr_list *node = NULL; + + /*merge the rootfs config*/ + if (container_rootfs != NULL) { + if (!container->root) { + container->root = util_common_calloc_s(sizeof(oci_runtime_spec_root)); + if (!container->root) { + ERROR("Out of memory"); + goto out; + } + } + + if (container->root->path) { + free(container->root->path); + } + + container->root->path = util_strdup_s(container_rootfs); + } + + /* lxc.rootfs + * lxc.rootfs.options + */ + if (container->root || container->linux) { + node = trans_oci_root(container->root, container->linux); + if (node == NULL) { + ERROR("Failed to translate rootfs configure"); + goto out; + } + lcr_list_merge(lcr_conf, node); + } + + /* lxc.idmap */ + if (container->linux) { + node = trans_oci_linux(container->linux, seccomp); + if (node == NULL) { + ERROR("Failed to translate linux configure"); + goto out; + } + lcr_list_merge(lcr_conf, node); + } + + ret = 0; +out: + return ret; +} + +static int trans_hostname_hooks_process_mounts(struct lcr_list *lcr_conf, const oci_runtime_spec *container) +{ + int ret = -1; + + /* lxc.uts.name */ + struct lcr_list *node = trans_oci_hostname(container->hostname); + if (node == NULL) { + ERROR("Failed to translate hostname"); + goto out; + } + lcr_list_add_tail(lcr_conf, node); + + /* lxc.init_{u|g}id + * lxc.init_cmd + * lxc.environment + * lxc.cap.{drop/keep} + * lxc.limit.* + * lxc.aa_profile + * lxc.se_context + */ + node = trans_oci_process(container->process); + if (node == NULL) { + ERROR("Failed to translate hooks"); + goto out; + } + lcr_list_merge(lcr_conf, node); + + /* lxc.mount.entry */ + node = trans_oci_mounts(container); + if (node == NULL) { + ERROR("Failed to translate mount entry configure"); + goto out; + } + lcr_list_merge(lcr_conf, node); + + ret = 0; +out: + return ret; +} + +static bool do_stop_and_wait(struct lxc_container *c, long timeout, bool force) +{ + pid_t pid; + int sret = 0; + bool ret = true; + + pid = c->init_pid(c); + if (pid < 1) { + ERROR("Container is not running"); + return false; + } + + if (!force) { + sret = kill(pid, SIGTERM); + if (sret < 0) { + if (errno == ESRCH) { + return true; + } + } + ret = c->wait(c, "STOPPED", (int)timeout); + if (ret) { + return true; + } + } + + sret = kill(pid, SIGKILL); + if (sret < 0) { + if (errno == ESRCH) { + return true; + } + } + ret = c->wait(c, "STOPPED", -1); + if (!ret) { + ERROR("Failed to stop container %s", c->name); + } + return ret; +} + +static bool do_stop(struct lxc_container *c, long timeout, bool force) +{ + bool ret = true; + + if (force && timeout) { + ERROR("-k should not use with -T"); + return false; + } + + if (!force && timeout < -1) { + ERROR("Timeout should be >= -1"); + return false; + } + + if (!c->is_defined(c)) { + ERROR("No such container"); + ret = false; + goto out; + } + + if (!c->may_control(c)) { + ERROR("Insufficent privleges to contol"); + ret = false; + goto out; + } + + if (!c->is_running(c)) { + DEBUG("%s is already stopped", c->name); + goto out; + } + + ret = do_stop_and_wait(c, timeout, force); + +out: + return ret; +} + +static int lcr_spec_write_seccomp_line(int fd, const char *seccomp) +{ + size_t len; + char *line = NULL; + int ret = -1; + int nret; + + if (strlen(seccomp) > SIZE_MAX - strlen("lxc.seccomp.profile") - 3 - 1) { + ERROR("the length of lxc.seccomp is too long!"); + goto cleanup; + } + + len = strlen("lxc.seccomp.profile") + 3 + strlen(seccomp) + 1; + + line = util_common_calloc_s(len * sizeof(char)); + if (line == NULL) { + ERROR("Out of memory"); + goto cleanup; + } + + nret = sprintf_s(line, len, "%s = %s", "lxc.seccomp.profile", seccomp); + if (nret < 0) { + ERROR("Sprintf failed"); + goto cleanup; + } + if ((size_t)nret > len - 1) { + nret = (int)(len - 1); + } + line[nret] = '\n'; + if (write(fd, line, len) == -1) { + SYSERROR("Write failed"); + goto cleanup; + } + ret = 0; +cleanup: + free(line); + return ret; +} + +static char *lcr_save_seccomp_file(const char *bundle, const char *seccomp_conf) +{ + char seccomp[PATH_MAX] = { 0 }; + char *real_seccomp = NULL; + int fd = -1; + int nret; + ssize_t written_cnt; + + nret = sprintf_s(seccomp, sizeof(seccomp), "%s/seccomp", bundle); + if (nret < 0) { + goto cleanup; + } + + nret = util_ensure_path(&real_seccomp, seccomp); + if (nret < 0) { + ERROR("Failed to ensure path %s", seccomp); + goto cleanup; + } + + fd = util_open(real_seccomp, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE); + if (fd == -1) { + SYSERROR("Create file %s failed", real_seccomp); + goto cleanup; + } + + written_cnt = write(fd, seccomp_conf, strlen(seccomp_conf)); + close(fd); + if (written_cnt == -1) { + SYSERROR("write seccomp_conf failed"); + goto cleanup; + } + return real_seccomp; +cleanup: + free(real_seccomp); + return NULL; +} + +static struct lcr_container_info *info_new(struct lcr_container_info **info, size_t *size) +{ + struct lcr_container_info *m = NULL; + struct lcr_container_info *n = NULL; + size_t length = 0; + int nret = 0; + + if (*size > SIZE_MAX / sizeof(struct lcr_container_info) - 1) { + return NULL; + } + + length = (*size + 1) * sizeof(struct lcr_container_info); + + nret = mem_realloc((void **)&n, length, (void *)(*info), (*size) * sizeof(struct lcr_container_info)); + if (nret < 0) { + return NULL; + } + + *info = n; + m = *info + *size; + (*size)++; + + // *INDENT-OFF* + *m = (struct lcr_container_info) { + .name = NULL, .state = NULL, .interface = NULL, .ipv4 = NULL, .ipv6 = NULL, .ram = 0.0, .swap = 0.0, .init = -1 + }; + // *INDENT-ON* + return m; +} + +static void free_arr(char **array, size_t size) +{ + size_t i; + for (i = 0; i < size; i++) { + free(array[i]); + array[i] = NULL; + } + free(array); +} + +/* + * Get a complete list of active containers for a given lcrpath. + * return Number of containers, or -1 on error. + **/ +int lcr_list_active_containers(const char *lcrpath, struct lcr_container_info **info_arr) +{ + char **c = NULL; + int n = 0; + int nret = -1; + size_t info_size = 0; + const char *path = lcrpath ? lcrpath : LCRPATH; + + n = list_active_containers(path, &c, NULL); + if (n == -1) { + n = 0; + } + + nret = lcr_containers_info_get(path, info_arr, &info_size, c, 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; +} + +bool lcr_delete_with_force(const char *name, const char *lcrpath, bool force) +{ + struct lxc_container *c = NULL; + const char *path = lcrpath ? lcrpath : LCRPATH; + bool ret = false; + pid_t pid = 0; + + clear_error_message(&g_lcr_error); + if (name == NULL) { + ERROR("Missing container name"); + return false; + } + engine_set_log_prefix(name); + c = lcr_new_container(name, path); + if (c == NULL) { + engine_free_log_prefix(); + return false; + } + + if (c->is_running(c)) { + if (!force) { + 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); + goto out_put; + } else { + pid = c->init_pid(c); + if (pid < 1) { + ERROR("Container is not running"); + goto clean_delete; + } + + ret = do_stop(c, 0, true); + if (!ret) { + ERROR("Failed to stop container %s", name); + goto out_put; + } + } + } + +clean_delete: + ret = lcr_clean(name, path, NULL, NULL, pid); + if (!ret) { + ERROR("Failed to clean resource"); + } + ret = lcr_delete(name, path); + if (!ret) { + ERROR("Failed to delete container"); + } + +out_put: + lxc_container_put(c); + engine_free_log_prefix(); + return ret; +} + +void lcr_free_config(struct lcr_list *lcr_conf) +{ + struct lcr_list *it = NULL; + struct lcr_list *next = NULL; + + if (lcr_conf == NULL) { + return; + } + + lcr_list_for_each_safe(it, lcr_conf, next) { + lcr_list_del(it); + free_lcr_list_node(it); + } +} + +int lcr_containers_info_get(const char *lcrpath, struct lcr_container_info **info, size_t *size, char **containers, + int num) +{ + struct lcr_container_info *in = NULL; + struct lxc_container *c = NULL; + int i; + int nret = -1; + + if ((lcrpath == NULL) || num == 0) { + goto err_out; + } + + for (i = 0; i < num; i++) { + const char *st = NULL; + const char *name = containers[i]; + bool run_flag = true; + if (name == NULL) { + continue; + } + + c = lxc_container_without_config_new(name, lcrpath); + if (c == NULL) { + continue; + } + + if (!c->is_defined(c)) { + goto put_container; + } + + st = c->state(c); + if (st == NULL) { + st = "UNKNOWN"; + } + run_flag = (strcmp(st, "STOPPED") != 0); + + /*Now it makes sense to allocate memory */ + in = info_new(info, size); + if (in == NULL) { + goto put_container; + } + in->running = run_flag; + in->name = util_strdup_s(name); + in->state = util_strdup_s(st); + if (run_flag) { + in->init = c->init_pid(c); + } +put_container: + lxc_container_put(c); + } + nret = 0; +err_out: + free_arr(containers, (size_t)num); + return nret; +} + +/* + *Transform container JSON into oci_runtime_spec struct +*/ +bool container_parse(const char *oci_filename, const char *oci_json_data, oci_runtime_spec **container) +{ + struct parser_context ctx = { OPT_PARSE_STRICT, stderr }; + parser_error err = NULL; + bool ret = true; + + if (oci_json_data == NULL) { + *container = oci_runtime_spec_parse_file(oci_filename, &ctx, &err); + } else { + *container = oci_runtime_spec_parse_data(oci_json_data, &ctx, &err); + } + + if (*container == NULL) { + ERROR("Failed to get OCI spec: %s", err); + ret = false; + goto out_free; + } +out_free: + free(err); + return ret; +} + +static int merge_annotations(const oci_runtime_spec *container, struct lcr_list *lcr_conf) +{ + int ret = 0; + struct lcr_list *tmp = NULL; + + if (container->annotations != NULL) { + tmp = trans_annotations(container->annotations); + if (tmp == NULL) { + ERROR("Failed to translate annotations configure"); + ret = -1; + goto out; + } + lcr_list_merge(lcr_conf, tmp); + } + +out: + return ret; +} + +static int merge_needed_lxc_conf(struct lcr_list *lcr_conf) +{ + int ret = 0; + + struct lcr_list *tmp = get_needed_lxc_conf(); + if (tmp == NULL) { + ERROR("Failed to append other lxc configure"); + ret = -1; + goto out; + } + lcr_list_merge(lcr_conf, tmp); + +out: + return ret; +} + +struct lcr_list *lcr_oci2lcr(const struct lxc_container *c, const char *container_rootfs, oci_runtime_spec *container, + char **seccomp) +{ + struct lcr_list *lcr_conf = NULL; + + lcr_conf = util_common_calloc_s(sizeof(*lcr_conf)); + if (lcr_conf == NULL) { + goto out_free; + } + lcr_list_init(lcr_conf); + + if (check_annotations(container, c)) { + ERROR("Check annotations failed"); + goto out_free; + } + + if (trans_rootfs_linux(lcr_conf, container_rootfs, container, seccomp)) { + goto out_free; + } + + if (trans_hostname_hooks_process_mounts(lcr_conf, container)) { + goto out_free; + } + + /* annotations.files.limit */ + if (merge_annotations(container, lcr_conf) != 0) { + goto out_free; + } + + /* Append other lxc configurations */ + if (merge_needed_lxc_conf(lcr_conf) != 0) { + goto out_free; + } + + return lcr_conf; + +out_free: + lcr_free_config(lcr_conf); + free(lcr_conf); + + return NULL; +} + +static inline bool is_distribution_ubuntu(const char *distribution) +{ + return strcmp("ubuntu", distribution) == 0; +} + +static inline bool is_distribution_app(const char *distribution) +{ + return strcmp("app", distribution) == 0; +} + +struct lcr_list *lcr_dist2spec(const char *distribution, char **seccomp_conf) +{ + const lcr_config_item_t *items = NULL; + const lcr_config_item_t *i = NULL; + struct lcr_list *lcr_conf = NULL; + struct lcr_list *tmp = NULL; + + if (seccomp_conf == NULL) { + return NULL; + } + + free(*seccomp_conf); + *seccomp_conf = NULL; + + /* get distribution specific configuration */ + if (is_distribution_ubuntu(distribution)) { + items = g_ubuntu_config_items; + *seccomp_conf = util_strdup_s(g_common_seccomp); + } else if (is_distribution_app(distribution)) { + items = g_app_config_items; + } else { + ERROR("Distribution \"%s\" is not supported", distribution); + return NULL; + } + + /* initialize the config list */ + lcr_conf = util_common_calloc_s(sizeof(*lcr_conf)); + if (lcr_conf == NULL) { + goto out_free; + } + lcr_list_init(lcr_conf); + + /* add other config items to `lcr_conf` list */ + for (i = items; i->name; i++) { + tmp = create_lcr_list_node(i->name, i->value); + if (tmp == NULL) { + goto out_free; + } + lcr_list_add_tail(lcr_conf, tmp); + } + + return lcr_conf; + +out_free: + ERROR("Failed translate dist to spec"); + free(*seccomp_conf); + *seccomp_conf = NULL; + + lcr_free_config(lcr_conf); + free(lcr_conf); + + return NULL; +} + +static int lcr_open_config_file(const char *bundle) +{ + char config[PATH_MAX] = { 0 }; + char *real_config = NULL; + int fd = -1; + int nret; + + nret = sprintf_s(config, sizeof(config), "%s/config", bundle); + if (nret < 0) { + goto out; + } + + nret = util_ensure_path(&real_config, config); + if (nret < 0) { + ERROR("Failed to ensure path %s", config); + goto out; + } + + fd = util_open(real_config, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE); + if (fd == -1) { + ERROR("Create file %s failed, %s", real_config, strerror(errno)); + lcr_set_error_message(LCR_ERR_RUNTIME, "Create file %s failed, %s", real_config, strerror(errno)); + goto out; + } +out: + free(real_config); + return fd; +} + +static int lcr_spec_write_config(int fd, const struct lcr_list *lcr_conf) +{ + struct lcr_list *it = NULL; + size_t len; + char *line = NULL; + int ret = -1; + + lcr_list_for_each(it, lcr_conf) { + lcr_config_item_t *item = it->elem; + int nret; + if (item != NULL) { + if (strlen(item->value) > ((SIZE_MAX - strlen(item->name)) - 4)) { + goto cleanup; + } + len = strlen(item->name) + 3 + strlen(item->value) + 1; + line = util_common_calloc_s(len); + if (line == NULL) { + ERROR("Out of memory"); + goto cleanup; + } + + nret = sprintf_s(line, len, "%s = %s", item->name, item->value); + if (nret < 0) { + ERROR("Sprintf failed"); + goto cleanup; + } + if ((size_t)nret > len - 1) { + nret = (int)(len - 1); + } + line[nret] = '\n'; + if (write(fd, line, len) == -1) { + SYSERROR("Write failed"); + goto cleanup; + } + free(line); + line = NULL; + } + } + ret = 0; +cleanup: + free(line); + return ret; +} + +char *lcr_get_bundle(const char *lcrpath, const char *name) +{ + size_t len = 0; + int nret = 0; + char *bundle = NULL; + struct stat s; + + if (strlen(name) > ((SIZE_MAX - strlen(lcrpath)) - 2)) { + goto cleanup; + } + + /* bundle = lcrpath + '/' + name + '\0' */ + len = strlen(lcrpath) + strlen(name) + 2; + bundle = util_common_calloc_s(len); + if (bundle == NULL) { + ERROR("Out of memory"); + goto cleanup; + } + + nret = sprintf_s(bundle, len, "%s/%s", lcrpath, name); + if (nret < 0) { + ERROR("Print bundle string failed"); + goto cleanup; + } + + if (stat(bundle, &s) != 0) { + switch (errno) { + case EACCES: + ERROR("You lack permission to access %s", bundle); + break; + case ENOENT: + ERROR("Bundle %s does not exist", bundle); + break; + default: + ERROR("Access %s failed: %s\n", bundle, strerror(errno)); + } + goto cleanup; + } + return bundle; +cleanup: + free(bundle); + return NULL; +} + +bool lcr_save_spec(const char *name, const char *lcrpath, const struct lcr_list *lcr_conf, const char *seccomp_conf) +{ + bool bret = false; + const char *path = lcrpath ? lcrpath : LCRPATH; + char *bundle = NULL; + char *seccomp = NULL; + int fd = -1; + int nret = 0; + + if (name == NULL) { + ERROR("Missing container name"); + return bret; + } + + bundle = lcr_get_bundle(path, name); + if (bundle == NULL) { + goto out_free; + } + + if (seccomp_conf != NULL) { + seccomp = lcr_save_seccomp_file(bundle, seccomp_conf); + if (seccomp == NULL) { + goto out_free; + } + } + + fd = lcr_open_config_file(bundle); + if (fd == -1) { + goto out_free; + } + + if (lcr_spec_write_config(fd, lcr_conf)) { + goto out_free; + } + + if (seccomp_conf != NULL) { + nret = lcr_spec_write_seccomp_line(fd, seccomp); + if (nret) { + goto out_free; + } + } + + bret = true; + +out_free: + free(bundle); + free(seccomp); + if (fd != -1) { + close(fd); + } + + return bret; +} diff --git a/src/lcrcontainer_extend.h b/src/lcrcontainer_extend.h new file mode 100644 index 0000000..b5271f6 --- /dev/null +++ b/src/lcrcontainer_extend.h @@ -0,0 +1,99 @@ +/****************************************************************************** + * 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 + ******************************************************************************/ +#ifndef __LCR_CONTAINER_EXTEND_H +#define __LCR_CONTAINER_EXTEND_H + +#include + +#include "oci_runtime_spec.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* defined in `lcr_list.h` */ +struct lcr_list; + +#define SAFE_MALLOC(P, size, ret) \ + do { \ + (P) = util_common_calloc_s((size)); \ + if ((P) == NULL) { \ + ERROR("Out of memory"); \ + (ret) = false; \ + } \ + } while (0); + +/* + * Get a complete list of active containers for a given lcrpath. + * return Number of containers, or -1 on error. + */ +int lcr_list_active_containers(const char *lcrpath, struct lcr_container_info **info_arr); + +/* + * Delete a container + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param force : force to delete container + */ +bool lcr_delete_with_force(const char *name, const char *lcrpath, bool force); + +/* + * Free lcr_conf + */ +void lcr_free_config(struct lcr_list *lcr_conf); + +bool container_parse(const char *oci_filename, const char *oci_json_data, oci_runtime_spec **container); + +/* + * Translate oci specification to lcr configuration. + * You should pass oci_filename or oci_spec to this function. + * param oci_filename : oci spec filename, in json format + * param oci_json_data : json string of oci config data + * param new_container : return newest oci_runtime_spec struct + * param seccomp : return seccomp parsed from oci spec + * return: a linked list + */ +struct lcr_list *lcr_oci2lcr(const struct lxc_container *c, const char *container_rootfs, + oci_runtime_spec *new_container, + char **seccomp); + +/* + * Generate lcr_config_info according to distribution. + * param distribution : system distribution, now support: ubuntu + * param seccomp_conf [out] : return seccomp configuration if needed. + * return: a linked list + */ +struct lcr_list *lcr_dist2spec(const char *distribution, char **seccomp_conf); + +/* + * Create a new specification file + * param name : container name, required. + * param lcrpath : container path, set to NULL if you want use default lcrpath. + * param lcr_conf : generate specification according to lcr_conf list + * param seccomp_conf : seccomp_conf will be wrote into seccomp file, set it to NULL if you don't need + */ +bool lcr_save_spec(const char *name, const char *lcrpath, const struct lcr_list *lcr_conf, const char *seccomp_conf); + +int lcr_containers_info_get(const char *lcrpath, struct lcr_container_info **info, size_t *size, char **containers, + int num); + +char *lcr_get_bundle(const char *lcrpath, const char *name); + + +#ifdef __cplusplus +} +#endif + +#endif /* __LCR_CONTAINER_EXTEND_H */ diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..7c5adae --- /dev/null +++ b/src/log.c @@ -0,0 +1,373 @@ +/****************************************************************************** + * 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 log functions + ******************************************************************************/ +#define _GNU_SOURCE +#define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "securec.h" +#include "log.h" +#include "utils.h" + +const char * const g_engine_log_prio_name[] = { + "FATAL", "ALERT", "CRIT", "ERROR", "WARN", + "NOTICE", "INFO", "DEBUG", "TRACE" +}; + +#define MAX_MSG_LENGTH 4096 +#define MAX_LOG_PREFIX_LENGTH 15 + +static __thread char *g_engine_log_prefix = NULL; + +static char *g_engine_log_vmname = NULL; +static bool g_engine_log_quiet = false; +static int g_engine_log_level = ENGINE_LOG_DEBUG; +static int g_engine_log_driver = LOG_DRIVER_STDOUT; +int g_engine_log_fd = -1; + +/* engine set log prefix */ +void engine_set_log_prefix(const char *prefix) +{ + if (prefix == NULL) { + return; + } + + free(g_engine_log_prefix); + + g_engine_log_prefix = util_strdup_s(prefix); +} + +/* engine free log prefix */ +void engine_free_log_prefix() +{ + free(g_engine_log_prefix); + + g_engine_log_prefix = NULL; +} + +ssize_t write_nointr(int fd, const void *buf, size_t count); + +void log_append_logfile(const struct engine_log_object_metadata *meta, const char *timestamp, const char *msg); +void log_append_stderr(const struct engine_log_object_metadata *meta, const char *timestamp, const char *msg); + +/* engine change str logdriver to enum */ +int engine_change_str_logdriver_to_enum(const char *driver) +{ + if (driver == NULL) { + return LOG_DRIVER_NOSET; + } + if (!strcasecmp(driver, "stdout")) { + return LOG_DRIVER_STDOUT; + } + if (!strcasecmp(driver, "fifo")) { + return LOG_DRIVER_FIFO; + } + + return -1; +} + +#define LOG_FIFO_SIZE (1024 * 1024) + +/* open fifo */ +int open_fifo(const char *fifo_path) +{ + int nret; + int fifo_fd = -1; + + nret = mknod(fifo_path, S_IFIFO | S_IRUSR | S_IWUSR, (dev_t)0); + if (nret && errno != EEXIST) { + printf("Mknod failed: %s\n", strerror(errno)); + return nret; + } + + fifo_fd = util_open(fifo_path, O_RDWR | O_NONBLOCK, 0); + if (fifo_fd == -1) { + fprintf(stderr, "Open fifo %s failed: %s\n", fifo_path, strerror(errno)); + return -1; + } + + if (fcntl(fifo_fd, F_SETPIPE_SZ, LOG_FIFO_SIZE) == -1) { + fprintf(stderr, "Set fifo buffer size failed: %s", strerror(errno)); + close(fifo_fd); + return -1; + } + + return fifo_fd; +} + +/* engine close log file */ +void engine_close_log_file() +{ + if (g_engine_log_fd != -1) { + close(g_engine_log_fd); + g_engine_log_fd = -1; + } +} + +/* init log driver */ +static int init_log_driver(const struct engine_log_config *log) +{ + int i, driver; + + for (i = ENGINE_LOG_FATAL; i < ENGINE_LOG_MAX; i++) { + if (!strcasecmp(g_engine_log_prio_name[i], log->priority)) { + g_engine_log_level = i; + break; + } + } + + if (i == ENGINE_LOG_MAX) { + fprintf(stderr, "Unable to parse logging level:%s\n", log->priority); + return -1; + } + + driver = engine_change_str_logdriver_to_enum(log->driver); + if (driver < 0) { + fprintf(stderr, "Invalid log driver: %s\n", log->driver); + return -1; + } + g_engine_log_driver = driver; + return 0; +} + +/* log enable */ +int log_enable(const struct engine_log_config *log) +{ + int nret = 0; + char *full_path = NULL; + + if ((log->name == NULL) || (log->priority == NULL)) { + return -1; + } + + if (g_engine_log_fd != -1) { + fprintf(stderr, "engine log already initialized\n"); + return 0; + } + if (init_log_driver(log)) { + return -1; + } + + free(g_engine_log_vmname); + + g_engine_log_vmname = util_strdup_s(log->name); + + g_engine_log_quiet = log->quiet; + + if ((log->file == NULL) || strcmp(log->file, "none") == 0) { + if (g_engine_log_driver == LOG_DRIVER_FIFO) { + fprintf(stderr, "Must set log file for driver %s\n", log->driver); + nret = -1; + } + goto out; + } + full_path = util_strdup_s(log->file); + if (full_path == NULL) { + fprintf(stderr, "Out of memory\n"); + nret = -1; + goto out; + } + + if (util_build_dir(full_path)) { + fprintf(stderr, "failed to create dir for log file\n"); + nret = -1; + goto out; + } + g_engine_log_fd = open_fifo(full_path); + + if (g_engine_log_fd == -1) { + nret = -1; + } +out: + if (nret) { + if (g_engine_log_driver == LOG_DRIVER_FIFO) { + g_engine_log_driver = LOG_DRIVER_NOSET; + } + } + free(full_path); + + return nret; +} + +static char *parse_timespec_to_human() +{ + struct timespec timestamp; + struct tm ptm = { 0 }; + char date_time[ENGINE_LOG_TIME_SIZE] = { 0 }; + int nret; + + if (clock_gettime(CLOCK_REALTIME, ×tamp) == -1) { + fprintf(stderr, "Failed to get real time\n"); + return 0; + } + + if (localtime_r(&(timestamp.tv_sec), &ptm) == NULL) { + SYSERROR("Transfer timespec failed"); + return NULL; + } + + nret = sprintf_s(date_time, ENGINE_LOG_TIME_SIZE, "%04d%02d%02d%02d%02d%02d.%03ld", + ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec, + timestamp.tv_nsec / 1000000); + + if (nret < 0) { + COMMAND_ERROR("Sprintf failed"); + return NULL; + } + + return util_strdup_s(date_time); +} + +/* engine log append */ +int engine_log_append(const struct engine_log_object_metadata *meta, const char *format, ...) +{ + int rc; + va_list args; + char msg[MAX_MSG_LENGTH] = { 0 }; + char *date_time = NULL; + int ret = 0; + + va_start(args, format); + rc = vsprintf_s(msg, MAX_MSG_LENGTH, format, args); + va_end(args); + if (rc < 0 || rc >= MAX_MSG_LENGTH) { + rc = sprintf_s(msg, MAX_MSG_LENGTH, "%s", "!!LONG LONG A LOG!!"); + if (rc < 0) { + return 0; + } + } + + date_time = parse_timespec_to_human(); + if (date_time == NULL) { + goto out; + } + + switch (g_engine_log_driver) { + case LOG_DRIVER_STDOUT: + if (g_engine_log_quiet) { + break; + } + log_append_stderr(meta, date_time, msg); + break; + case LOG_DRIVER_FIFO: + if (g_engine_log_fd == -1) { + fprintf(stderr, "Do not set log file\n"); + ret = -1; + goto out; + } + log_append_logfile(meta, date_time, msg); + break; + case LOG_DRIVER_NOSET: + break; + default: + fprintf(stderr, "Invalid log driver\n"); + ret = -1; + goto out; + } + +out: + free(date_time); + return ret; +} + +/* log append logfile */ +void log_append_logfile(const struct engine_log_object_metadata *meta, const char *timestamp, const char *msg) +{ + char log_buffer[ENGINE_LOG_BUFFER_SIZE] = { 0 }; + int log_fd = -1; + int nret; + size_t size; + char *tmp_prefix = NULL; + + if (meta->level > g_engine_log_level) { + return; + } + log_fd = g_engine_log_fd; + if (log_fd == -1) { + return; + } + + tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_vmname; + if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) { + tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH); + } + nret = sprintf_s(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "", + timestamp, g_engine_log_prio_name[meta->level], + g_engine_log_vmname ? g_engine_log_vmname : "engine", meta->file, meta->func, + meta->line, msg); + + if (nret < 0) { + nret = sprintf_s(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", + tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[meta->level], + g_engine_log_vmname ? g_engine_log_vmname : "engine", meta->file, + meta->func, meta->line, "Large log message"); + if (nret < 0) { + return; + } + } + size = (size_t)nret; + + if (size > (sizeof(log_buffer) - 1)) { + size = sizeof(log_buffer) - 1; + } + + log_buffer[size] = '\n'; + + if (write_nointr(log_fd, log_buffer, (size + 1)) == -1) { + fprintf(stderr, "write log into logfile failed"); + } +} + +/* log append stderr */ +void log_append_stderr(const struct engine_log_object_metadata *meta, const char *timestamp, const char *msg) +{ + char *tmp_prefix = NULL; + if (meta->level > g_engine_log_level) { + return; + } + + tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_vmname; + if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) { + tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH); + } + fprintf(stderr, "%15s %s %-8s ", tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[meta->level]); + fprintf(stderr, "%s - ", g_engine_log_vmname ? g_engine_log_vmname : "engine"); + fprintf(stderr, "%s:%s:%d - ", meta->file, meta->func, meta->line); + fprintf(stderr, "%s", msg); + fprintf(stderr, "\n"); +} + +/* write nointr */ +ssize_t write_nointr(int fd, const void *buf, size_t count) +{ + ssize_t nret; + for (;;) { + nret = write(fd, buf, count); + if (nret < 0 && errno == EINTR) { + continue; + } else { + break; + } + } + return nret; +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..9a8c7b5 --- /dev/null +++ b/src/log.h @@ -0,0 +1,146 @@ +/****************************************************************************** + * 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 log functions + ******************************************************************************/ +#ifndef __LCR_LOG_H +#define __LCR_LOG_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef O_CLOEXEC +#define O_CLOEXEC 02000000 +#endif + +#ifndef F_DUPFD_CLOEXEC +#define F_DUPFD_CLOEXEC 1030 +#endif + +#define ENGINE_LOG_BUFFER_SIZE 4096 + +/* We're logging in seconds and nanoseconds. Assuming that the underlying + * datatype is currently at maximum a 64bit integer, we have a date string that + * is of maximum length (2^64 - 1) * 2 = (21 + 21) = 42. + * */ +#define ENGINE_LOG_TIME_SIZE 42 + +/*errmsg that defined in lcrc & lcrd*/ +#define DAEMON_ERROR_GRPC_INIT_STR "Init failed" +#define DAEMON_ERROR_GRPC_CONNENCT_STR "Can not connect with server.Is the docker dameon running on the host?" +#define DAEMON_ERROR_GRPC_SERVER_STR "Server internal error" + +enum engine_log_level { + ENGINE_LOG_FATAL = 0, + ENGINE_LOG_ALERT, + ENGINE_LOG_CRIT, + ENGINE_LOG_ERROR, + ENGINE_LOG_WARN, + ENGINE_LOG_NOTICE, + ENGINE_LOG_INFO, + ENGINE_LOG_DEBUG, + ENGINE_LOG_TRACE, + ENGINE_LOG_MAX +}; + +enum g_engine_log_driver { + LOG_DRIVER_STDOUT, + LOG_DRIVER_FIFO, + LOG_DRIVER_NOSET, +}; + +struct engine_log_config { + const char *name; + const char *file; + const char *priority; + const char *prefix; + const char *driver; + bool quiet; +}; + +#define ENGINE_LOG_LOCINFO_INIT \ + { \ + .file = __FILE__, .func = __func__, .line = __LINE__, \ + } + +/* brief logging object metadata */ +struct engine_log_object_metadata { + int level; + + /* location information of the logging item */ + const char *file; + const char *func; + int line; +}; + +extern void engine_close_log_file(); +int log_enable(const struct engine_log_config *log); + +void engine_set_log_prefix(const char *prefix); + +void engine_free_log_prefix(); + +int engine_change_str_logdriver_to_enum(const char *driver); + +int engine_log_append(const struct engine_log_object_metadata *event, const char *format, ...); + +#define COMMON_LOG(loglevel, format, ...) \ + do { \ + struct engine_log_object_metadata meta = ENGINE_LOG_LOCINFO_INIT; \ + meta.level = loglevel; \ + (void)engine_log_append(&meta, format, ##__VA_ARGS__); \ + } while (0) + +#define DEBUG(format, ...) \ + COMMON_LOG(ENGINE_LOG_FATAL, format, ##__VA_ARGS__); + +#define INFO(format, ...) \ + COMMON_LOG(ENGINE_LOG_INFO, format, ##__VA_ARGS__); + +#define NOTICE(format, ...) \ + COMMON_LOG(ENGINE_LOG_NOTICE, format, ##__VA_ARGS__); + +#define WARN(format, ...) \ + COMMON_LOG(ENGINE_LOG_WARN, format, ##__VA_ARGS__); + +#define ERROR(format, ...) \ + COMMON_LOG(ENGINE_LOG_ERROR, format, ##__VA_ARGS__); + +#define CRIT(format, ...) \ + COMMON_LOG(ENGINE_LOG_CRIT, format, ##__VA_ARGS__); + +#define ALERT(format, ...) \ + COMMON_LOG(ENGINE_LOG_ALERT, format, ##__VA_ARGS__); + +#define FATAL(format, ...) \ + COMMON_LOG(ENGINE_LOG_FATAL, format, ##__VA_ARGS__); + +#define SYSERROR(format, ...) \ + do { \ + ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \ + } while (0) + +#define COMMAND_ERROR(fmt, args...) \ + do { \ + (void)fprintf(stderr, fmt "\n", ##args); \ + } while (0) + +#ifdef __cplusplus +} +#endif + +#endif /* __LCR_LOG_H */ diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..41fbba2 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,1657 @@ +/****************************************************************************** + * 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 utils functions + ******************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "constants.h" +#include "utils.h" +#include "securec.h" +#include "log.h" + +#define ISSLASH(C) ((C) == '/') +#define IS_ABSOLUTE_FILE_NAME(F) (ISSLASH((F)[0])) +#define IS_RELATIVE_FILE_NAME(F) (!IS_ABSOLUTE_FILE_NAME(F)) + +static bool do_clean_path_continue(const char *endpos, const char *stpos, const char *respath, char **dst) +{ + if (endpos - stpos == 1 && stpos[0] == '.') { + return true; + } else if (endpos - stpos == 2 && stpos[0] == '.' && stpos[1] == '.') { + char *dest = *dst; + if (dest <= respath + 1) { + return true; + } + for (--dest; dest > respath && !ISSLASH(dest[-1]); --dest) { + continue; + } + *dst = dest; + return true; + } + return false; +} + +static int do_clean_path(const char *respath, const char *limit_respath, const char *stpos, char **dst) +{ + char *dest = *dst; + const char *endpos = stpos; + errno_t ret; + + for (; *stpos; stpos = endpos) { + while (ISSLASH(*stpos)) { + ++stpos; + } + + for (endpos = stpos; *endpos && !ISSLASH(*endpos); ++endpos) { + } + + if (endpos - stpos == 0) { + break; + } else if (do_clean_path_continue(endpos, stpos, respath, &dest)) { + continue; + } + + if (!ISSLASH(dest[-1])) { + *dest++ = '/'; + } + + if (dest + (endpos - stpos) >= limit_respath) { + ERROR("Path is too long"); + if (dest > respath + 1) { + dest--; + } + *dest = '\0'; + return -1; + } + + ret = memcpy_s(dest, (size_t)(endpos - stpos), stpos, (size_t)(endpos - stpos)); + if (ret != EOK) { + ERROR("Failed at cleanpath memcpy"); + return -1; + } + dest += endpos - stpos; + *dest = '\0'; + } + *dst = dest; + return 0; +} + +static char *cleanpath(const char *path, char *realpath, size_t realpath_len) +{ + char *respath = NULL; + char *dest = NULL; + const char *stpos = NULL; + const char *limit_respath = NULL; + errno_t ret; + + if (path == NULL || path[0] == '\0' || realpath == NULL || (realpath_len < PATH_MAX)) { + return NULL; + } + + respath = realpath; + + ret = memset_s(respath, realpath_len, 0, realpath_len); + if (ret != EOK) { + ERROR("Failed at cleanpath memset"); + goto error; + } + limit_respath = respath + PATH_MAX; + + if (!IS_ABSOLUTE_FILE_NAME(path)) { + if (!getcwd(respath, PATH_MAX)) { + ERROR("Failed to getcwd"); + respath[0] = '\0'; + goto error; + } + dest = strchr(respath, '\0'); + if (dest == NULL) { + ERROR("Failed to get the end of respath"); + goto error; + } + ret = strcat_s(respath, PATH_MAX, path); + if (ret != EOK) { + ERROR("Failed at cleanpath strcat"); + goto error; + } + stpos = path; + } else { + dest = respath; + *dest++ = '/'; + stpos = path; + } + + if (do_clean_path(respath, limit_respath, stpos, &dest)) { + goto error; + } + + if (dest > respath + 1 && ISSLASH(dest[-1])) { + --dest; + } + *dest = '\0'; + + return respath; + +error: + return NULL; +} + +bool file_exists(const char *path) +{ + struct stat s; + + if (path == NULL) { + return false; + } + + return stat(path, &s) == 0; +} + +/* dir exists */ +bool dir_exists(const char *path) +{ + struct stat s; + int nret; + + if (path == NULL) { + return false; + } + + nret = stat(path, &s); + if (nret < 0) { + return false; + } + + return S_ISDIR(s.st_mode); +} + +/* wait for pid */ +int wait_for_pid(pid_t pid) +{ + int st; + int nret = 0; + +again: + nret = waitpid(pid, &st, 0); + if (nret == -1) { + if (errno == EINTR) { + goto again; + } + return -1; + } + if (nret != pid) { + goto again; + } + if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) { + return -1; + } + return 0; +} + +/* wait for pid status */ +int wait_for_pid_status(pid_t pid) +{ + int st; + int nret = 0; +rep: + nret = waitpid(pid, &st, 0); + if (nret == -1) { + if (errno == EINTR) { + goto rep; + } + return -1; + } + + if (nret != pid) { + goto rep; + } + return st; +} + +static char *do_string_join(const char *sep, const char **parts, size_t parts_len, size_t result_len) +{ + size_t iter; + + char *res_string = calloc(result_len + 1, 1); + if (res_string == NULL) { + return NULL; + } + + for (iter = 0; iter < parts_len - 1; iter++) { + if (strcat_s(res_string, result_len + 1, parts[iter]) != EOK) { + free(res_string); + return NULL; + } + if (strcat_s(res_string, result_len + 1, sep) != EOK) { + free(res_string); + return NULL; + } + } + if (strcat_s(res_string, result_len + 1, parts[parts_len - 1]) != EOK) { + free(res_string); + return NULL; + } + return res_string; +} + +char *util_string_join(const char *sep, const char **parts, size_t len) +{ + size_t sep_len; + size_t result_len; + size_t iter; + + if (len == 0 || parts == NULL || sep == NULL) { + return NULL; + } + + sep_len = strlen(sep); + + if ((sep_len != 0) && (sep_len != 1) && (len > SIZE_MAX / sep_len + 1)) { + return NULL; + } + result_len = (len - 1) * sep_len; + for (iter = 0; iter < len; iter++) { + if (parts[iter] == NULL || result_len >= SIZE_MAX - strlen(parts[iter])) { + return NULL; + } + result_len += strlen(parts[iter]); + } + + return do_string_join(sep, parts, len, result_len); +} + +int util_mkdir_p(const char *dir, mode_t mode) +{ + const char *tmp_pos = NULL; + const char *base = NULL; + char *cur_dir = NULL; + ssize_t len = 0; + + if (dir == NULL || strlen(dir) > PATH_MAX) { + goto err_out; + } + + tmp_pos = dir; + base = dir; + + do { + dir = tmp_pos + strspn(tmp_pos, "/"); + tmp_pos = dir + strcspn(dir, "/"); + len = dir - base; + if (len <= 0) { + break; + } + cur_dir = strndup(base, (size_t)len); + if (cur_dir == NULL) { + ERROR("strndup failed"); + goto err_out; + } + if (*cur_dir) { + if (mkdir(cur_dir, mode) && (errno != EEXIST || !util_dir_exists(cur_dir))) { + ERROR("failed to create directory '%s': %s", cur_dir, strerror(errno)); + goto err_out; + } + } + free(cur_dir); + } while (tmp_pos != dir); + + return 0; +err_out: + free(cur_dir); + return -1; +} + +/* lcr shrink array*/ +static char **lcr_shrink_array(char **orig_array, size_t new_size) +{ + char **res_array = NULL; + size_t i; + + if (new_size == 0) { + return orig_array; + } + + if (new_size > SIZE_MAX / sizeof(char *)) { + return orig_array; + } + res_array = (char **)util_common_calloc_s(new_size * sizeof(char *)); + if (res_array == NULL) { + return orig_array; + } + + for (i = 0; i < new_size; i++) { + res_array[i] = orig_array[i]; + } + free(orig_array); + return res_array; +} + +/* lcr string split and trim*/ +char **lcr_string_split_and_trim(const char *orig_str, char _sep) +{ + char *token = NULL; + char *str = NULL; + char *reserve_ptr = NULL; + char deli[2] = { _sep, '\0' }; + char **res_array = NULL; + size_t capacity = 0; + size_t count = 0; + int r, tmp_errno; + + if (orig_str == NULL) { + return calloc(1, sizeof(char *)); + } + + str = util_strdup_s(orig_str); + + token = strtok_r(str, deli, &reserve_ptr); + while (token != NULL) { + while (token[0] == ' ' || token[0] == '\t') { + token++; + } + size_t len = strlen(token); + while (len > 0 && (token[len - 1] == ' ' || token[len - 1] == '\t')) { + token[len - 1] = '\0'; + len--; + } + r = lcr_grow_array((void ***)&res_array, &capacity, count + 1, 16); + if (r < 0) { + goto error_out; + } + res_array[count] = util_strdup_s(token); + count++; + token = strtok_r(NULL, deli, &reserve_ptr); + } + free(str); + + return lcr_shrink_array(res_array, count + 1); + +error_out: + tmp_errno = errno; + lcr_free_array((void **)res_array); + free(str); + errno = tmp_errno; + return NULL; +} + +/* lcr free array*/ +void lcr_free_array(void **orig_array) +{ + void **p = NULL; + for (p = orig_array; p && *p; p++) { + free(*p); + *p = NULL; + } + free((void *)orig_array); +} + +/* lcr grow array*/ +int lcr_grow_array(void ***orig_array, size_t *orig_capacity, size_t size, size_t increment) +{ + size_t add_capacity; + void **add_array = NULL; + + if (orig_array == NULL || orig_capacity == NULL || size == SIZE_MAX) { + return -1; + } + + if ((*orig_array) == NULL || (*orig_capacity) == 0) { + free(*orig_array); + *orig_array = NULL; + *orig_capacity = 0; + } + + add_capacity = *orig_capacity; + while (size + 1 > add_capacity) { + if (add_capacity > SIZE_MAX - increment) { + return -1; + } + add_capacity += increment; + } + if (add_capacity != *orig_capacity) { + if (add_capacity > SIZE_MAX / sizeof(void *)) { + return -1; + } + add_array = util_common_calloc_s(add_capacity * sizeof(void *)); + if (add_array == NULL) { + return -1; + } + if (*orig_array) { + if (memcpy_s(add_array, add_capacity * sizeof(void *), *orig_array, *orig_capacity * sizeof(void *)) != + EOK) { + ERROR("Failed to memcpy memory"); + free(add_array); + return -1; + } + free((void *)*orig_array); + } + + *orig_array = add_array; + *orig_capacity = add_capacity; + } + + return 0; +} + +/* lcr array len*/ +size_t lcr_array_len(void **orig_array) +{ + void **p = NULL; + size_t length = 0; + + for (p = orig_array; p && *p; p++) { + length++; + } + + return length; +} + +/* util common malloc s*/ +void *util_common_calloc_s(size_t size) +{ + if (size == 0) { + return NULL; + } + + return calloc(1, size); +} + +int mem_realloc(void **new, size_t newsize, void *old, size_t oldsize) +{ + int nret = 0; + void *addr = NULL; + + if (new == NULL) { + return -1; + } + + if (oldsize >= newsize || newsize == 0) { + goto err_out; + } + + addr = util_common_calloc_s(newsize); + if (addr == NULL) { + goto err_out; + } + + if (old != NULL) { + nret = memcpy_s(addr, newsize, old, oldsize); + if (nret != EOK) { + free(addr); + goto err_out; + } + free(old); + } + + *new = addr; + return 0; + +err_out: + return -1; +} + +bool util_valid_cmd_arg(const char *arg) +{ + return arg && strchr(arg, '|') == NULL && strchr(arg, '`') == NULL && strchr(arg, '&') == NULL && + strchr(arg, ';') == NULL; +} + +static inline bool is_invalid_error_str(const char *err_str, const char *numstr) +{ + return err_str == NULL || err_str == numstr || *err_str != '\0'; +} + +int util_safe_strtod(const char *numstr, double *converted) +{ + char *err_str = NULL; + double ld; + + if (numstr == NULL || converted == NULL) { + return -EINVAL; + } + + errno = 0; + ld = strtod(numstr, &err_str); + if (errno > 0) { + return -errno; + } + + if (is_invalid_error_str(err_str, numstr)) { + return -EINVAL; + } + + *converted = ld; + return 0; +} + +/* util safe ullong*/ +int util_safe_ullong(const char *numstr, unsigned long long *converted) +{ + char *err_str = NULL; + unsigned long long sli; + + if (numstr == NULL || converted == NULL) { + return -EINVAL; + } + + errno = 0; + sli = strtoull(numstr, &err_str, 0); + if (errno > 0) { + return -errno; + } + + if (is_invalid_error_str(err_str, numstr)) { + return -EINVAL; + } + + *converted = (unsigned long long)sli; + return 0; +} + +/* util safe uint*/ +int util_safe_uint(const char *numstr, unsigned int *converted) +{ + char *err_str = NULL; + unsigned long int ui; + + if (numstr == NULL || converted == NULL) { + return -EINVAL; + } + + errno = 0; + ui = strtoul(numstr, &err_str, 0); + if (errno > 0) { + return -errno; + } + + if (is_invalid_error_str(err_str, numstr)) { + return -EINVAL; + } + + if (ui > UINT_MAX) { + return -ERANGE; + } + + *converted = (unsigned int)ui; + return 0; +} + +struct unit_map_def_lcr { + int64_t mltpl; + char *name; +}; + +static struct unit_map_def_lcr const g_lcr_unit_map[] = { + { .mltpl = 1, .name = "I" }, { .mltpl = 1, .name = "B" }, { .mltpl = 1, .name = "IB" }, + { .mltpl = SIZE_KB, .name = "K" }, { .mltpl = SIZE_KB, .name = "KI" }, { .mltpl = SIZE_KB, .name = "KB" }, + { .mltpl = SIZE_KB, .name = "KIB" }, { .mltpl = SIZE_MB, .name = "M" }, { .mltpl = SIZE_MB, .name = "MI" }, + { .mltpl = SIZE_MB, .name = "MB" }, { .mltpl = SIZE_MB, .name = "MIB" }, { .mltpl = SIZE_GB, .name = "G" }, + { .mltpl = SIZE_GB, .name = "GI" }, { .mltpl = SIZE_GB, .name = "GB" }, { .mltpl = SIZE_GB, .name = "GIB" }, + { .mltpl = SIZE_TB, .name = "T" }, { .mltpl = SIZE_TB, .name = "TI" }, { .mltpl = SIZE_TB, .name = "TB" }, + { .mltpl = SIZE_TB, .name = "TIB" }, { .mltpl = SIZE_PB, .name = "P" }, { .mltpl = SIZE_PB, .name = "PI" }, + { .mltpl = SIZE_PB, .name = "PB" }, { .mltpl = SIZE_PB, .name = "PIB" } +}; + +static size_t const g_lcr_unit_map_len = sizeof(g_lcr_unit_map) / sizeof(g_lcr_unit_map[0]); + +/* parse unit multiple*/ +static int parse_unit_multiple(const char *unit, int64_t *mltpl) +{ + size_t i; + if (unit[0] == '\0') { + *mltpl = 1; + return 0; + } + + for (i = 0; i < g_lcr_unit_map_len; i++) { + if (strcasecmp(unit, g_lcr_unit_map[i].name) == 0) { + *mltpl = g_lcr_unit_map[i].mltpl; + return 0; + } + } + return -EINVAL; +} + +static int util_parse_size_int_and_float(const char *numstr, int64_t mlt, int64_t *converted) +{ + long long int_size = 0; + double float_size = 0; + long long int_real = 0; + long long float_real = 0; + int nret; + + char *dot = strchr(numstr, '.'); + if (dot != NULL) { + char tmp; + // interger.float + if (dot == numstr || *(dot + 1) == '\0') { + return -EINVAL; + } + // replace 123.456 to 120.456 + tmp = *(dot - 1); + *(dot - 1) = '0'; + // parsing 0.456 + nret = util_safe_strtod(dot - 1, &float_size); + // recover 120.456 to 123.456 + *(dot - 1) = tmp; + if (nret < 0) { + return nret; + } + float_real = (int64_t)float_size; + if (mlt > 0) { + if (INT64_MAX / mlt < (int64_t)float_size) { + return -ERANGE; + } + float_real = (int64_t)(float_size * mlt); + } + *dot = '\0'; + } + nret = util_safe_llong(numstr, &int_size); + if (nret < 0) { + return nret; + } + int_real = int_size; + if (mlt > 0) { + if (INT64_MAX / mlt < int_size) { + return -ERANGE; + } + int_real = int_size * mlt; + } + if (INT64_MAX - int_real < float_real) { + return -ERANGE; + } + + *converted = int_real + float_real; + return 0; +} + +/* parse byte size string*/ +int parse_byte_size_string(const char *s, int64_t *converted) +{ + int ret; + int64_t mltpl = 0; + char *dup = NULL; + char *pmlt = NULL; + + if (converted == NULL || s == NULL || s[0] == '\0' || !isdigit(s[0])) { + return -EINVAL; + } + + dup = util_strdup_s(s); + + pmlt = dup; + while (*pmlt != '\0' && (isdigit(*pmlt) || *pmlt == '.')) { + pmlt++; + } + + ret = parse_unit_multiple(pmlt, &mltpl); + if (ret) { + free(dup); + return ret; + } + + // replace the first multiple arg to '\0' + *pmlt = '\0'; + ret = util_parse_size_int_and_float(dup, mltpl, converted); + free(dup); + return ret; +} + +/* + * if path do not exist, this function will create it. + */ +int util_ensure_path(char **confpath, const char *path) +{ + int err = -1; + int fd; + char real_path[PATH_MAX + 1] = { 0 }; + + if (confpath == NULL || path == NULL) { + return -1; + } + + fd = util_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, DEFAULT_SECURE_FILE_MODE); + if (fd < 0 && errno != EEXIST) { + ERROR("failed to open '%s'", path); + goto err; + } + if (fd >= 0) { + close(fd); + } + + if (strlen(path) > PATH_MAX || realpath(path, real_path) == NULL) { + ERROR("Failed to get real path: %s", path); + goto err; + } + + *confpath = util_strdup_s(real_path); + + err = EXIT_SUCCESS; + +err: + return err; +} + +/* util dir exists*/ +bool util_dir_exists(const char *path) +{ + struct stat s; + int nret; + + if (path == NULL) { + return false; + } + + nret = stat(path, &s); + if (nret < 0) { + return false; + } + + return S_ISDIR(s.st_mode); +} + +static inline bool is_dot(const char *value) +{ + return strcmp(value, ".") == 0; +} + +static inline bool is_double_dot(const char *value) +{ + return strcmp(value, "..") == 0; +} + + +/* util rmdir one*/ +static void util_rmdir_one(const char *dirpath, const struct dirent *pdirent, int recursive_depth, int *failure) +{ + struct stat fstat; + int nret; + char fname[PATH_MAX] = { 0 }; + + if (is_dot(pdirent->d_name) || is_double_dot(pdirent->d_name)) { + return; + } + + nret = sprintf_s(fname, PATH_MAX, "%s/%s", dirpath, pdirent->d_name); + if (nret < 0) { + ERROR("Pathname too long"); + *failure = 1; + return; + } + + nret = lstat(fname, &fstat); + if (nret) { + ERROR("Failed to stat %s", fname); + *failure = 1; + return; + } + + if (S_ISDIR(fstat.st_mode)) { + if (util_recursive_rmdir(fname, (recursive_depth + 1)) < 0) { + *failure = 1; + } + } else { + if (unlink(fname) < 0) { + ERROR("Failed to delete %s", fname); + *failure = 1; + } + } +} + +/* util recursive rmdir*/ +int util_recursive_rmdir(const char *dirpath, int recursive_depth) +{ + struct dirent *pdirent = NULL; + DIR *directory = NULL; + int nret; + int failure = 0; + + if ((recursive_depth + 1) > MAX_PATH_DEPTH) { + ERROR("Reach max path depth: %s", dirpath); + failure = 1; + goto err_out; + } + + if (!util_dir_exists(dirpath)) { /*dir not exists*/ + goto err_out; + } + + directory = opendir(dirpath); + if (directory == NULL) { + ERROR("Failed to open %s", dirpath); + failure = 1; + goto err_out; + } + + pdirent = readdir(directory); + while (pdirent != NULL) { + util_rmdir_one(dirpath, pdirent, recursive_depth, &failure); + pdirent = readdir(directory); + } + + if (rmdir(dirpath) < 0) { + ERROR("Failed to delete %s", dirpath); + failure = 1; + } + + nret = closedir(directory); + if (nret) { + ERROR("Failed to close directory %s", dirpath); + failure = 1; + } + +err_out: + return failure ? -1 : 0; +} + +/* util string replace one*/ +static ssize_t util_string_replace_one(const char *needle, const char *replace, const char *haystack, char **result) +{ + char *res_string = *result; + char *p = NULL; + char *next_p = NULL; + ssize_t length = 0; + size_t replace_len, nl_len, part_len; + + replace_len = strlen(replace); + nl_len = strlen(needle); + + for (next_p = (char *)haystack, p = strstr(next_p, needle); p != NULL; next_p = p, p = strstr(next_p, needle)) { + part_len = (size_t)(p - next_p); + if ((res_string != NULL) && part_len > 0) { + if (memcpy_s(&res_string[length], part_len, next_p, part_len) != EOK) { + ERROR("Failed to copy memory!"); + return -1; + } + } + length += (ssize_t)part_len; + if ((res_string != NULL) && replace_len > 0) { + if (memcpy_s(&res_string[length], replace_len, replace, replace_len) != EOK) { + ERROR("Failed to copy memory!"); + return -1; + } + } + length += (ssize_t)replace_len; + p += nl_len; + } + part_len = strlen(next_p); + if ((res_string != NULL) && part_len > 0) { + if (memcpy_s(&res_string[length], part_len, next_p, part_len) != EOK) { + ERROR("Failed to copy memory!"); + return -1; + } + } + length += (ssize_t)part_len; + return length; +} + +/* util string replace*/ +char *util_string_replace(const char *needle, const char *replace, const char *haystack) +{ + ssize_t length = -1; + ssize_t reserve_len = -1; + char *res_string = NULL; + + if ((needle == NULL) || (replace == NULL) || (haystack == NULL)) { + ERROR("Invalid NULL pointer"); + return NULL; + } + + while (length == -1 || res_string == NULL) { + if (length != -1) { + res_string = calloc(1, (size_t)length + 1); + if (res_string == NULL) { + return NULL; + } + reserve_len = length; + } + length = util_string_replace_one(needle, replace, haystack, &res_string); + if (length < 0) { + free(res_string); + return NULL; + } + } + + if (reserve_len != length) { + free(res_string); + return NULL; + } + if (res_string[length] != '\0') { + free(res_string); + return NULL; + } + + return res_string; +} + +int util_open(const char *filename, int flags, mode_t mode) +{ + char rpath[PATH_MAX] = { 0x00 }; + + if (cleanpath(filename, rpath, sizeof(rpath)) == NULL) { + return -1; + } + if (mode) { + return open(rpath, flags | O_CLOEXEC, mode); + } else { + return open(rpath, flags | O_CLOEXEC); + } +} + +FILE *util_fopen(const char *filename, const char *mode) +{ + unsigned int fdmode = 0; + int f_fd = -1; + int tmperrno; + FILE *fp = NULL; + char rpath[PATH_MAX] = { 0x00 }; + + if (mode == NULL) { + return NULL; + } + + if (cleanpath(filename, rpath, sizeof(rpath)) == NULL) { + ERROR("cleanpath failed"); + return NULL; + } + if (!strncmp(mode, "a+", 2)) { + fdmode = O_RDWR | O_CREAT | O_APPEND; + } else if (!strncmp(mode, "a", 1)) { + fdmode = O_WRONLY | O_CREAT | O_APPEND; + } else if (!strncmp(mode, "w+", 2)) { + fdmode = O_RDWR | O_TRUNC | O_CREAT; + } else if (!strncmp(mode, "w", 1)) { + fdmode = O_WRONLY | O_TRUNC | O_CREAT; + } else if (!strncmp(mode, "r+", 2)) { + fdmode = O_RDWR; + } else if (!strncmp(mode, "r", 1)) { + fdmode = O_RDONLY; + } + + fdmode |= O_CLOEXEC; + + f_fd = open(rpath, (int)fdmode, 0666); + if (f_fd < 0) { + return NULL; + } + + fp = fdopen(f_fd, mode); + tmperrno = errno; + if (fp == NULL) { + close(f_fd); + } + errno = tmperrno; + return fp; +} + +/* util safe int*/ +int util_safe_int(const char *numstr, int *converted) +{ + char *err_str = NULL; + signed long int li; + + if (numstr == NULL || converted == NULL) { + return -EINVAL; + } + + errno = 0; + li = strtol(numstr, &err_str, 0); + if (errno > 0) { + return -errno; + } + + if (is_invalid_error_str(err_str, numstr)) { + return -EINVAL; + } + + if (li > INT_MAX) { + return -ERANGE; + } + + *converted = (int)li; + return 0; +} + +/* util check inherited*/ +static bool util_dir_skip_current(const struct dirent *pdirent) +{ + if (is_dot(pdirent->d_name)) { + return true; + } + + if (is_double_dot(pdirent->d_name)) { + return true; + } + return false; +} + +static bool util_is_std_fileno(int fd) +{ + return fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO; +} + +int util_check_inherited(bool closeall, int fd_to_ignore) +{ + struct dirent *pdirent = NULL; + int fd = -1; + int fddir = -1; + DIR *directory = NULL; + +restart: + if (directory != NULL) { + closedir(directory); + } + directory = opendir("/proc/self/fd"); + if (directory == NULL) { + WARN("Failed to open directory: %m."); + return -1; + } + + fddir = dirfd(directory); + pdirent = readdir(directory); + for (; pdirent != NULL; pdirent = readdir(directory)) { + if (util_dir_skip_current(pdirent)) { + continue; + } + + if (util_safe_int(pdirent->d_name, &fd) < 0) { + continue; + } + + if (util_is_std_fileno(fd) || fd == fddir || fd == fd_to_ignore) { + continue; + } + + if (closeall) { + if (fd >= 0) { + close(fd); + fd = -1; + } + goto restart; + } + } + + closedir(directory); + return 0; +} + +/* util string append */ +char *util_string_append(const char *dst, const char *src) +{ + char *res_string = NULL; + size_t length = 0; + + if ((dst == NULL) && (src == NULL)) { + return NULL; + } + if (src == NULL) { + return util_strdup_s(dst); + } + if (dst == NULL) { + return util_strdup_s(src); + } + + if (strlen(dst) > ((SIZE_MAX - strlen(src)) - 1)) { + return NULL; + } + length = strlen(dst) + strlen(src) + 1; + res_string = util_common_calloc_s(length); + if (res_string == NULL) { + return NULL; + } + + if (sprintf_s(res_string, length, "%s%s", src, dst) < 0) { + ERROR("Failed to get mount option"); + free(res_string); + return NULL; + } + + return res_string; +} + +/* util string split prefix */ +char *util_string_split_prefix(size_t prefix_len, const char *file) +{ + size_t file_len = 0; + size_t len = 0; + char *path = NULL; + + if (file == NULL) { + return NULL; + } + + file_len = strlen(file); + if (file_len < prefix_len) { + return NULL; + } + len = strlen(file) - prefix_len; + if (len > SIZE_MAX / sizeof(char) - 1) { + return NULL; + } + path = util_common_calloc_s((len + 1) * sizeof(char)); + if (path == NULL) { + return NULL; + } + if (strncpy_s(path, len + 1, file + prefix_len, len) != EOK) { + ERROR("Failed to copy string!"); + free(path); + return NULL; + } + path[len] = '\0'; + + return path; +} + +static void set_char_to_terminator(char *p) +{ + *p = '\0'; +} +/* + * @name is absolute path of this file. + * make all directory in this absolute path. + * */ +int util_build_dir(const char *name) +{ + char *n = NULL; // because we'll be modifying it + char *p = NULL; + char *e = NULL; + int nret; + + if (name == NULL) { + return -1; + } + + n = util_strdup_s(name); + e = &(n[strlen(n)]); + for (p = n + 1; p < e; p++) { + if (*p != '/') { + continue; + } + set_char_to_terminator(p); + if (access(n, F_OK)) { + nret = mkdir(n, DEFAULT_SECURE_DIRECTORY_MODE); + if (nret && (errno != EEXIST || !dir_exists(n))) { + ERROR("failed to create directory '%s'.", n); + free(n); + return -1; + } + } + *p = '/'; + } + free(n); + return 0; +} + +/* util write nointr */ +ssize_t util_write_nointr(int fd, const void *buf, size_t count) +{ + ssize_t nret; + + if (buf == NULL) { + return -1; + } + + for (;;) { + nret = write(fd, buf, count); + if (nret < 0 && errno == EINTR) { + continue; + } else if (nret < 0 && errno == EAGAIN) { + continue; + } else { + break; + } + } + return nret; +} + +/* util read nointr */ +ssize_t util_read_nointr(int fd, void *buf, size_t count) +{ + ssize_t nret; + + if (buf == NULL) { + return -1; + } + + for (;;) { + nret = read(fd, buf, count); + if (nret < 0 && errno == EINTR) { + continue; + } else { + break; + } + } + + return nret; +} + +/* util free array */ +void util_free_array(char **array) +{ + char **p = NULL; + + if (array == NULL) { + return; + } + + for (p = array; p && *p; p++) { + free(*p); + *p = NULL; + } + free((void *)array); +} + +/* sig num */ +static int sig_num(const char *sig) +{ + int n; + + if (util_safe_int(sig, &n) < 0) { + return -1; + } + + return n; +} + +struct signame { + int num; + const char *name; +}; + +/* util sig parse */ +int util_sig_parse(const char *signame) +{ + size_t n; + const struct signame signames[] = SIGNAL_MAP_DEFAULT; + + if (signame == NULL) { + return -1; + } + + if (isdigit(*signame)) { + return sig_num(signame); + } else if (strncasecmp(signame, "sig", 3) == 0) { + signame += 3; + for (n = 0; n < sizeof(signames) / sizeof(signames[0]); n++) { + if (strcasecmp(signames[n].name, signame) == 0) { + return signames[n].num; + } + } + } else { + for (n = 0; n < sizeof(signames) / sizeof(signames[0]); n++) { + if (strcasecmp(signames[n].name, signame) == 0) { + return signames[n].num; + } + } + } + + return -1; +} + +/* util valid signal */ +bool util_valid_signal(int sig) +{ + size_t n = 0; + const struct signame signames[] = SIGNAL_MAP_DEFAULT; + + for (n = 0; n < sizeof(signames) / sizeof(signames[0]); n++) { + if (signames[n].num == sig) { + return true; + } + } + + return false; +} + +/* str skip str */ +const char *str_skip_str(const char *str, const char *skip) +{ + if (str == NULL || skip == NULL) { + return NULL; + } + + for (;; str++, skip++) { + if (!*skip) { + return str; + } else if (*str != *skip) { + return NULL; + } + } +} + +/* util array len */ +size_t util_array_len(char **array) +{ + char **pos = NULL; + size_t len = 0; + + for (pos = array; pos && *pos; pos++) { + len++; + } + + return len; +} + +/* util array append */ +int util_array_append(char ***array, const char *element) +{ + size_t len; + char **new_array = NULL; + + if (array == NULL || element == NULL) { + return -1; + } + + // let newlen to len + 2 for element and null + len = util_array_len(*array); + if (len > SIZE_MAX / sizeof(char *) - 2) { + ERROR("Array size is too big!"); + return -1; + } + new_array = util_common_calloc_s((len + 2) * sizeof(char *)); + if (new_array == NULL) { + ERROR("Out of memory"); + return -1; + } + if (*array) { + if (memcpy_s(new_array, (len + 2) * sizeof(char *), *array, len * sizeof(char *)) != EOK) { + ERROR("Failed to memcpy memory"); + free(new_array); + return -1; + } + free((void *)*array); + } + *array = new_array; + + new_array[len] = util_strdup_s(element); + + return 0; +} + +/* util safe llong */ +int util_safe_llong(const char *numstr, long long *converted) +{ + char *err_str = NULL; + long long ll; + + if (numstr == NULL || converted == NULL) { + return -EINVAL; + } + + errno = 0; + ll = strtoll(numstr, &err_str, 0); + if (errno > 0) { + return -errno; + } + + if (is_invalid_error_str(err_str, numstr)) { + return -EINVAL; + } + + *converted = (long long)ll; + return 0; +} + +char *util_strdup_s(const char *src) +{ + char *dst = NULL; + + if (src == NULL) { + return NULL; + } + + dst = strdup(src); + if (dst == NULL) { + abort(); + } + + return dst; +} + +static int open_devnull(void) +{ + int fd = open("/dev/null", O_RDWR); + if (fd < 0) { + SYSERROR("Can't open /dev/null"); + } + + return fd; +} + +static int set_stdfds(int fd) +{ + int ret = 0; + + if (fd < 0) { + return -1; + } + + ret = dup2(fd, STDIN_FILENO); + if (ret < 0) { + return -1; + } + + ret = dup2(fd, STDOUT_FILENO); + if (ret < 0) { + return -1; + } + + ret = dup2(fd, STDERR_FILENO); + if (ret < 0) { + return -1; + } + + return 0; +} + +int util_null_stdfds(void) +{ + int ret = -1; + int fd; + + fd = open_devnull(); + if (fd >= 0) { + ret = set_stdfds(fd); + close(fd); + } + + return ret; +} + +bool util_copy_file(const char *src_file, const char *dst_file) +{ + bool ret = false; + char *nret = NULL; + char real_src_file[PATH_MAX + 1] = { 0 }; + int src_fd = -1; + int dst_fd = -1; + char buf[BUFSIZE + 1] = { 0 }; + + if (src_file == NULL || dst_file == NULL) { + return ret; + } + nret = realpath(src_file, real_src_file); + if (nret == NULL) { + ERROR("real path: %s, return: %s", src_file, strerror(errno)); + return ret; + } + src_fd = util_open(real_src_file, O_RDONLY, CONFIG_FILE_MODE); + if (src_fd < 0) { + ERROR("Open src file: %s, failed: %s", real_src_file, strerror(errno)); + goto free_out; + } + dst_fd = util_open(dst_file, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_SECURE_FILE_MODE); + if (dst_fd < 0) { + ERROR("Creat file: %s, failed: %s", dst_file, strerror(errno)); + goto free_out; + } + while (true) { + ssize_t len = util_read_nointr(src_fd, buf, BUFSIZE); + if (len < 0) { + ERROR("Read src file failed: %s", strerror(errno)); + goto free_out; + } else if (len == 0) { + break; + } + if (util_write_nointr(dst_fd, buf, (size_t)len) != len) { + ERROR("Write file failed: %s", strerror(errno)); + goto free_out; + } + } + ret = true; +free_out: + if (src_fd >= 0) { + close(src_fd); + } + if (dst_fd >= 0) { + close(dst_fd); + } + return ret; +} + +bool util_write_file(const char *filepath, const char *content, size_t len, bool add_newline) +{ + bool ret = true; + int rfd = -1; + + if (filepath == NULL || content == NULL) { + return false; + } + + rfd = util_open(filepath, O_CREAT | O_TRUNC | O_WRONLY, CONFIG_FILE_MODE); + if (rfd == -1) { + ERROR("Create file %s failed: %s", filepath, strerror(errno)); + return false; + } + if (write(rfd, content, len) == -1) { + ERROR("Write hostname failed: %s", strerror(errno)); + ret = false; + goto out_free; + } + + if (add_newline && write(rfd, "\n", 1) == -1) { + ERROR("Write new line failed: %s", strerror(errno)); + ret = false; + goto out_free; + } + +out_free: + close(rfd); + return ret; +} + +static void util_trim_newline(char *s) +{ + if (s == NULL) { + return; + } + size_t len = strlen(s); + while ((len > 1) && (s[len - 1] == '\n')) { + s[--len] = '\0'; + } +} + +static int append_new_content_to_file(FILE *fp, const char *content) +{ + int ret = 0; + size_t length = 0; + size_t content_len = 0; + char *line = NULL; + char *tmp_str = NULL; + bool need_append = true; + while (getline(&line, &length, fp) != -1) { + if (line == NULL) { + ERROR("Failed to read content from file ptr"); + ret = -1; + goto out; + } + util_trim_newline(line); + if (!strcmp(content, line)) { + need_append = false; + break; + } + } + if (need_append) { + if (strlen(content) > ((SIZE_MAX - strlen("\n")) - 1)) { + ret = -1; + goto out; + } + content_len = strlen(content) + strlen("\n") + 1; + tmp_str = util_common_calloc_s(content_len); + if (tmp_str == NULL) { + ERROR("Out of memory"); + ret = -1; + goto out; + } + if (sprintf_s(tmp_str, content_len, "%s\n", content) < 0) { + ERROR("Failed to print string"); + ret = -1; + goto out; + } + if (fwrite(tmp_str, 1, strlen(tmp_str), fp) == 0) { + ERROR("Failed to write content: '%s'", content); + ret = -1; + goto out; + } + } + +out: + free(tmp_str); + free(line); + return ret; +} + +int util_atomic_write_file(const char *filepath, const char *content) +{ + int fd; + int ret = 0; + FILE *fp = NULL; + struct flock lk; + + if (filepath == NULL || content == NULL) { + return -1; + } + fd = util_open(filepath, O_RDWR | O_CREAT | O_APPEND, DEFAULT_SECURE_FILE_MODE); + if (fd < 0) { + ERROR("Failed to open: %s", filepath); + return -1; + } + 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) { + fp = fdopen(fd, "a+"); + if (fp == NULL) { + ERROR("Failed to open fd: %d", fd); + ret = -1; + goto out; + } + ret = append_new_content_to_file(fp, content); + } +out: + if (fp != NULL) { + fclose(fp); + } + close(fd); + return ret; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..81eacac --- /dev/null +++ b/src/utils.h @@ -0,0 +1,202 @@ +/****************************************************************************** + * 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 utils functions + ******************************************************************************/ +#ifndef __LCR_UTILS_H +#define __LCR_UTILS_H + +#include +#include +#include +#include + +#ifndef O_CLOEXEC +#define O_CLOEXEC 02000000 +#endif + +#define ECOMMON 1 +#define EINVALIDARGS 125 +#define ECMDNOTFOUND 127 + +#define LCR_NUMSTRLEN64 21 + +#define SPACE_MAGIC_STR "[#)" + +#define MAX_PATH_DEPTH 1024 + +#define SIZE_KB 1024LL +#define SIZE_MB (1024LL * SIZE_KB) +#define SIZE_GB (1024LL * SIZE_MB) +#define SIZE_TB (1024LL * SIZE_GB) +#define SIZE_PB (1024LL * SIZE_TB) + +#define BUFSIZE 4096 + +#ifndef SIGTRAP +#define SIGTRAP 5 +#endif + +#ifndef SIGIOT +#define SIGIOT 6 +#endif + +#ifndef SIGEMT +#define SIGEMT 7 +#endif + +#ifndef SIGBUS +#define SIGBUS 7 +#endif + +#ifndef SIGSTKFLT +#define SIGSTKFLT 16 +#endif + +#ifndef SIGCLD +#define SIGCLD 17 +#endif + +#ifndef SIGURG +#define SIGURG 23 +#endif + +#ifndef SIGXCPU +#define SIGXCPU 24 +#endif + +#ifndef SIGXFSZ +#define SIGXFSZ 25 +#endif + +#ifndef SIGVTALRM +#define SIGVTALRM 26 +#endif + +#ifndef SIGPROF +#define SIGPROF 27 +#endif + +#ifndef SIGWINCH +#define SIGWINCH 28 +#endif + +#ifndef SIGIO +#define SIGIO 29 +#endif + +#ifndef SIGPOLL +#define SIGPOLL 29 +#endif + +#ifndef SIGINFO +#define SIGINFO 29 +#endif + +#ifndef SIGLOST +#define SIGLOST 37 +#endif + +#ifndef SIGPWR +#define SIGPWR 30 +#endif + +#ifndef SIGUNUSED +#define SIGUNUSED 31 +#endif + +#ifndef SIGSYS +#define SIGSYS 31 +#endif + +#ifndef SIGRTMIN1 +#define SIGRTMIN1 34 +#endif + +#ifndef SIGRTMAX +#define SIGRTMAX 64 +#endif + +#define SIGNAL_MAP_DEFAULT \ + { \ + { SIGHUP, "HUP" }, { SIGINT, "INT" }, { SIGQUIT, "QUIT" }, { SIGILL, "ILL" }, { SIGABRT, "ABRT" }, \ + { SIGFPE, "FPE" }, { SIGKILL, "KILL" }, { SIGSEGV, "SEGV" }, { SIGPIPE, "PIPE" }, { SIGALRM, "ALRM" }, \ + { SIGTERM, "TERM" }, { SIGUSR1, "USR1" }, { SIGUSR2, "USR2" }, { SIGCHLD, "CHLD" }, \ + { SIGCONT, "CONT" }, { SIGSTOP, "STOP" }, { SIGTSTP, "TSTP" }, { SIGTTIN, "TTIN" }, \ + { SIGTTOU, "TTOU" }, { SIGTRAP, "TRAP" }, { SIGIOT, "IOT" }, { SIGEMT, "EMT" }, { SIGBUS, "BUS" }, \ + { SIGSTKFLT, "STKFLT" }, { SIGCLD, "CLD" }, { SIGURG, "URG" }, { SIGXCPU, "XCPU" }, \ + { SIGXFSZ, "XFSZ" }, { SIGVTALRM, "VTALRM" }, { SIGPROF, "PROF" }, { SIGWINCH, "WINCH" }, \ + { SIGIO, "IO" }, { SIGPOLL, "POLL" }, { SIGINFO, "INFO" }, { SIGLOST, "LOST" }, { SIGPWR, "PWR" }, \ + { SIGUNUSED, "UNUSED" }, { SIGSYS, "SYS" }, { SIGRTMIN, "RTMIN" }, { SIGRTMIN + 1, "RTMIN+1" }, \ + { SIGRTMIN + 2, "RTMIN+2" }, { SIGRTMIN + 3, "RTMIN+3" }, { SIGRTMIN + 4, "RTMIN+4" }, \ + { SIGRTMIN + 5, "RTMIN+5" }, { SIGRTMIN + 6, "RTMIN+6" }, { SIGRTMIN + 7, "RTMIN+7" }, \ + { SIGRTMIN + 8, "RTMIN+8" }, { SIGRTMIN + 9, "RTMIN+9" }, { SIGRTMIN + 10, "RTMIN+10" }, \ + { SIGRTMIN + 11, "RTMIN+11" }, { SIGRTMIN + 12, "RTMIN+12" }, { SIGRTMIN + 13, "RTMIN+13" }, \ + { SIGRTMIN + 14, "RTMIN+14" }, { SIGRTMIN + 15, "RTMIN+15" }, { SIGRTMAX - 14, "RTMAX-14" }, \ + { SIGRTMAX - 13, "RTMAX-13" }, { SIGRTMAX - 12, "RTMAX-12" }, { SIGRTMAX - 11, "RTMAX-11" }, \ + { SIGRTMAX - 10, "RTMAX-10" }, { SIGRTMAX - 9, "RTMAX-9" }, { SIGRTMAX - 8, "RTMAX-8" }, \ + { SIGRTMAX - 7, "RTMAX-7" }, { SIGRTMAX - 6, "RTMAX-6" }, { SIGRTMAX - 5, "RTMAX-5" }, \ + { SIGRTMAX - 4, "RTMAX-4" }, { SIGRTMAX - 3, "RTMAX-3" }, { SIGRTMAX - 2, "RTMAX-2" }, \ + { SIGRTMAX - 1, "RTMAX-1" }, { SIGRTMAX, "RTMAX" }, \ + } + +bool file_exists(const char *path); +bool dir_exists(const char *path); +int wait_for_pid(pid_t pid); +int wait_for_pid_status(pid_t pid); +char *util_string_join(const char *sep, const char **parts, size_t len); +int util_mkdir_p(const char *dir, mode_t mode); +char **lcr_string_split_and_trim(const char *str, char _sep); +void lcr_free_array(void **array); +int lcr_grow_array(void ***array, size_t *capacity, size_t new_size, size_t capacity_increment); +size_t lcr_array_len(void **array); +int mem_realloc(void **new, size_t newsize, void *old, size_t oldsize); +bool util_valid_cmd_arg(const char *arg); +int util_safe_ullong(const char *numstr, unsigned long long *converted); +int util_safe_strtod(const char *numstr, double *converted); +int util_safe_uint(const char *numstr, unsigned int *converted); +int parse_byte_size_string(const char *s, int64_t *converted); +bool util_dir_exists(const char *path); +int util_ensure_path(char **confpath, const char *path); +int util_recursive_rmdir(const char *dirpath, int recursive_depth); +char *util_string_replace(const char *needle, const char *replacement, const char *haystack); +int util_open(const char *filename, int flags, mode_t mode); + +FILE *util_fopen(const char *filename, const char *mode); + +void *util_common_calloc_s(size_t size); +int util_safe_int(const char *numstr, int *converted); +int util_check_inherited(bool closeall, int fd_to_ignore); +char *util_string_append(const char *dst, const char *src); +char *util_string_split_prefix(size_t prefix_len, const char *file); + +int util_build_dir(const char *name); +ssize_t util_write_nointr(int fd, const void *buf, size_t count); +ssize_t util_read_nointr(int fd, void *buf, size_t count); +void util_free_array(char **array); + +int util_sig_parse(const char *signame); +bool util_valid_signal(int sig); +size_t util_array_len(char **array); +const char *str_skip_str(const char *str, const char *skip); +int util_array_append(char ***array, const char *element); +int util_safe_llong(const char *numstr, long long *converted); +char *util_strdup_s(const char *src); +int util_null_stdfds(void); + +bool util_copy_file(const char *src_file, const char *dst_file); + +bool util_write_file(const char *filepath, const char *content, size_t len, bool add_newline); + +int util_atomic_write_file(const char *filepath, const char *content); + +#endif /*__LCR_UTILS_H */ diff --git a/tools/static_check b/tools/static_check new file mode 100755 index 0000000..bd531e5 --- /dev/null +++ b/tools/static_check @@ -0,0 +1,554 @@ +####################################################################### +##- @Copyright (C) Huawei Technologies., Ltd. 2017-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. +##- @Description: generate cetification +##- @Author: wujing +##- @Create: 2019-04-25 +####################################################################### +#!/bin/bash +# +# This script is the implementation portal for the iSulad project Personal level build static check. +# set -euxo pipefail + +CURRENT_PATH=$(pwd) +CI_TOOLS_PROJECT="/root/workspace/ci_tools" +export LOCAL_INCLUDE="/root/workspace/ci_tools/rule/include" +export EULER_CODE_PATH="$(realpath ${CURRENT_PATH}/..)" +LINT_RULE_FILE="/root/workspace/ci_tools/rule/pclint" +PCLINT_TOOL="/usr/local/bin/flint" +CODESTYLE_TOOL="/usr/local/bin/cpplint.py" +CMETRICS_TOOL="/root/cmetrics/cmetrics.py" + +function usage() { + echo -e "\ +=================================================================================================\033[1;37m + _____ ______ ___ ______ ____ ______ ______ __ __ ______ ______ __ __ + / ___//_ __// | /_ __// _// ____/ / ____// / / // ____// ____// //_/ + \__ \ / / / /| | / / / / / / / / / /_/ // __/ / / / ,< + ___/ / / / / ___ | / / _/ / / /___ / /___ / __ // /___ / /___ / /| | + /____/ /_/ /_/ |_|/_/ /___/ \____/ \____//_/ /_//_____/ \____//_/ |_| \033[0m +=================================================================================================" + echo "Usage: $0 [options]" + echo "Personal level build static check script for iSulad project" + echo "Options:" + echo " -u, --update-ci-tools Update ci tools project and replace header files with latest ones" + echo " -p, --pclint Perform pclint code static check" + echo " -s, --codestyle Perform codestyle(codedex) code static check" + echo " -c, --detail-cmetrics Detail code statistics analysis" + echo " -m, --simple-cmetrics Simple code statistics analysis" + echo " -a, --all Perform all checks and statistics" + echo " -i, --incremental-check Perform incremental check" + echo " -f, --quick-format Incremental format code by astyle/clang-format" + echo " -k, --style-check Check code style by astyle" + echo " --cpp-check Check code style by Cppcheck" + echo " -h, --help Script help information" +} + +function err() { + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2 +} + +function update_ci_tools() { + cd ${CI_TOOLS_PROJECT} + git fetch origin + git diff > static_check_backup.patch + git checkout . + git rebase origin/next_docker + cd ${CURRENT_PATH} + for file in $(find . -regextype posix-extended -regex ".*\.(h)") + do + cp $file $LOCAL_INCLUDE/docker/iSulad + done +} + +PCLINT_MASKED_RULE="679|826|726|322|571" + +function pclint_check() { + echo -e "\ +=================================================================================================\033[1;35m + ____ ______ __ ____ _ __ ______ ______ __ __ ______ ______ __ __ + / __ \ / ____// / / _// | / //_ __/ / ____// / / // ____// ____// //_/ + / /_/ // / / / / / / |/ / / / / / / /_/ // __/ / / / ,< + / ____// /___ / /___ _/ / / /| / / / / /___ / __ // /___ / /___ / /| | + /_/ \____//_____//___//_/ |_/ /_/ \____//_/ /_//_____/ \____//_/ |_| \033[0m +=================================================================================================" + local start_time=$(date +%s) + local files + if [[ ${1} == "all" ]]; then + files=$(find ./src -regextype posix-extended -regex ".*\.(c)") + else + files=$(git diff --name-only HEAD | grep -E "*.c$") + fi + files=(${files// / }) + local total=${#files[@]} + local failure_num=0 + local index=1 + for file in ${files[@]} + do + ${PCLINT_TOOL} -i ${LINT_RULE_FILE} std_lcr.lnt $file 2>&1 | grep -E "Err|Warn|Info" | grep -vE ${PCLINT_MASKED_RULE} + if [[ $? -eq 0 ]];then + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;31m\033[5m%s\033[0m\n" \ + ${index} ${total} ${file} "[FAILED]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + failure_num=$((failure_num+1)) + else + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;32m%-5s\033[0m\n" \ + ${index} ${total} ${file} "[PASS]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + fi + index=$((index+1)) + done + printf "%0.s=" {1..96} + printf "\n" + local end_time=$(date +%s) + local duration=$((${end_time} - ${start_time})) + echo -e "\033[1;36mTotal files: ${total}\033[0m, \033[1;32msuccess: $((total-failure_num))\033[0m, \033[1;31mfailure: ${failure_num}\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m" + if [[ ${failure_num} -ne 0 ]]; then + exit -1 + fi +} + +CODESTYLE_MASKED_RULE=( + "Start-processing" + "Done-processing" + "Total-errors-found" + "\[build/header_guard\]-\[5\]" + "\[build/c++11\]-\[5\]" + "\[whitespace/indent\]-\[3\]" + "\[whitespace/braces\]-\[4\]" + "\[readability/condition\]-\[2\]" + "\[whitespace/braces\]-\[5\]" + "\[build/c\+\+11\]-\[5\]" + "\[build/include_order\]-\[4\]" + "\[readability/multiline_string\]-\[5\]" + "\[runtime/string\]-\[4\]" + "\[whitespace/semicolon\]-\[5\]" + "\[whitespace/comments\]-\[2\]" + "\[build/c\+\+11\]-\[3\]" + "\[whitespace/operators\]-\[4\]" + "\[runtime/threadsafe_fn\]-\[2\]" + "\[runtime/printf\]-\[4\]" + "\[readability/alt_tokens\]-\[2\]" +) +function codestyle_check() { + echo -e "\ +=================================================================================================\033[1;33m + ______ ____ ____ ____ _ __ ______ _____ ________ __ __ ______ + / ____// __ \ / __ \ / _// | / // ____// ___//_ __/\ \/ // / / ____/ + / / / / / // / / / / / / |/ // / __ \__ \ / / \ // / / __/ + / /___ / /_/ // /_/ /_/ / / /| // /_/ / ___/ / / / / // /___ / /___ + \____/ \____//_____//___//_/ |_/ \____/ /____/ /_/ /_//_____//_____/\033[0m +=================================================================================================" + local masked_rule=$(echo ${CODESTYLE_MASKED_RULE[@]} | sed -e "s/ /|/g" -e "s/-/ /g") + local start_time=$(date +%s) + local files + if [[ ${1} == "all" ]]; then + files=$(find ./src -regextype posix-extended -regex ".*\.(h|c|cc)") + else + files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$") + fi + files=(${files// / }) + local total=${#files[@]} + local failure_num=0 + local index=1 + for file in ${files[@]} + do + python3 ${CODESTYLE_TOOL} $file 2>&1 | grep -vE "${masked_rule}" + if [[ $? -eq 0 ]];then + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;31m\033[5m%s\033[0m\n" \ + ${index} ${total} ${file} "[FAILED]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + failure_num=$((failure_num+1)) + else + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;32m%-5s\033[0m\n" \ + ${index} ${total} ${file} "[PASS]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + fi + index=$((index+1)) + done + printf "%0.s=" {1..96} + printf "\n" + local end_time=$(date +%s) + local duration=$((${end_time} - ${start_time})) + echo -e "\033[1;36mTotal files: ${total}\033[0m, \033[1;32msuccess: $((total-failure_num))\033[0m, \033[1;31mfailure: ${failure_num}\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m" + if [[ ${failure_num} -ne 0 ]]; then + exit -1 + fi +} + +CPPCHRECK_RULE=( + "information" + "warning" + "performance" + "style" + # "unusedFunction" + # "all" +) +CPPCHRCK_LOG="${CURRENT_PATH}/cppcheck.log" + +function cpp_check() { + echo -e "\ +=================================================================================================\033[1;33m + ______ ____ ____ ______ __ __ ______ ______ __ __ + / ____// __ \ / __ \ / ____// / / // ____// ____// //_/ + / / / /_/ // /_/ / / / / /_/ // __/ / / / ,< + / /___ / ____// ____/ / /___ / __ // /___ / /___ / /| | + \____//_/ /_/ \____//_/ /_//_____/ \____//_/ |_|\033[0m +=================================================================================================" + echo "cpp check is in progress, please wait a few seconds..." + printf "%0.s*" {1..97} + printf "\n" + local check_rule=$(echo ${CPPCHRECK_RULE[@]} | sed -e "s/ /,/g") + local start_time=$(date +%s) + result=$(cppcheck --enable=${check_rule} -I ./src -I ./build -i ./build -i ./isula_testcases -i ./CI ./ 2>&1 \ + | grep -vE "^Checking|done$|Cppcheck cannot find all the include files") + nums=$(echo "${result}" | wc -l) + echo "${result}" + local end_time=$(date +%s) + local duration=$((${end_time} - ${start_time})) + if [[ ${nums} -eq 0 ]] || [[ -z ${result} ]]; then + echo -e "\033[1;32mSuccess: clean code!\033[0m \033[1;33mSpend time: ${duration} seconds\033[0m" + else + printf "%0.s*" {1..97} + printf "\n" + echo -e "\033[1;31mFailure: There are ${nums} warnings that you need to handle\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m" + exit -1 + fi +} + +function clang_format() { + echo -e "\ +=================================================================================================\033[1;36m + ______ __ ___ _ __ ______ ______ ____ ____ __ ___ ___ ______ + / ____// / / | / | / // ____/ / ____// __ \ / __ \ / |/ // | /_ __/ + / / / / / /| | / |/ // / __ ______ / /_ / / / // /_/ // /|_/ // /| | / / + / /___ / /___ / ___ | / /| // /_/ //_____// __/ / /_/ // _, _// / / // ___ | / / + \____//_____//_/ |_|/_/ |_/ \____/ /_/ \____//_/ |_|/_/ /_//_/ |_|/_/ \033[0m] +=================================================================================================" + local start_time=$(date +%s) + local files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$") + files=(${files// / }) + local total=${#files[@]} + local failure_num=0 + local index=1 + for file in ${files[@]} + do + clang-format -i ${file} + if [[ $? -ne 0 ]];then + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;31m\033[5m%s\033[0m\n" \ + ${index} ${total} ${file} "[FAILED]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + failure_num=$((failure_num+1)) + else + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;32m%-5s\033[0m\n" \ + ${index} ${total} ${file} "[PASS]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + fi + index=$((index+1)) + done + printf "%0.s=" {1..96} + printf "\n" + local end_time=$(date +%s) + local duration=$((${end_time} - ${start_time})) + echo -e "\033[1;36mTotal files: ${total}\033[0m, \033[1;32msuccess: $((total-failure_num))\033[0m, \033[1;31mfailure: ${failure_num}\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m" +} + +function do_astyle_fix() { + astyle --options=none --lineend=linux --mode=c \ + --style=kr \ + --add-braces \ + --indent=spaces=4 \ + --indent-preprocessor \ + --indent-col1-comments \ + --indent-switches \ + --indent-cases \ + --min-conditional-indent=0 \ + --max-instatement-indent=120 \ + --max-code-length=120 \ + --break-after-logical \ + --pad-oper \ + --pad-header \ + --unpad-paren \ + --pad-comma \ + --lineend=linux \ + --align-reference=name \ + --close-templates \ + --indent-preproc-define \ + --indent-cases \ + --indent-switches \ + --attach-namespaces \ + --attach-classes \ + --attach-extern-c \ + --attach-closing-while \ + --indent-col1-comments \ + --break-one-line-headers \ + --close-templates < "${1}" +} + +function astyle_fix() { + [[ -z "${1}" || ! -r "${1}" ]] && exit -1 + tmp="$(mktemp --tmpdir=$(dirname "${1}"))" + do_astyle_fix "${1}" > "${tmp}" + sed -i 's/\*const/\* const/g' "${tmp}" + mv "${tmp}" "${1}" +} + +function astyle_format() { + echo -e "\ +=================================================================================================\033[1;36m + ___ _____ ________ __ __ ______ ______ ____ ____ __ ___ ___ ______ + / | / ___//_ __/\ \/ // / / ____/ / ____// __ \ / __ \ / |/ // | /_ __/ + / /| | \__ \ / / \ // / / __/ ______ / /_ / / / // /_/ // /|_/ // /| | / / + / ___ | ___/ / / / / // /___ / /___/_____// __/ / /_/ // _, _// / / // ___ | / / + /_/ |_|/____/ /_/ /_//_____//_____/ /_/ \____//_/ |_|/_/ /_//_/ |_|/_/ \033[0m] +=================================================================================================" + local start_time=$(date +%s) + local files=$(find ./src -regextype posix-extended -regex ".*\.(h|c|cc)") + files=(${files// / }) + local total=${#files[@]} + local failure_num=0 + local index=1 + for file in ${files[@]} + do + astyle_fix ${file} + if [[ $? -ne 0 ]];then + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;31m\033[5m%s\033[0m\n" \ + ${index} ${total} ${file} "[FAILED]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + failure_num=$((failure_num+1)) + else + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;32m%-5s\033[0m\n" \ + ${index} ${total} ${file} "[PASS]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + fi + index=$((index+1)) + done + printf "%0.s=" {1..96} + printf "\n" + local end_time=$(date +%s) + local duration=$((${end_time} - ${start_time})) + echo -e "\033[1;36mTotal files: ${total}\033[0m, \033[1;32msuccess: $((total-failure_num))\033[0m, \033[1;31mfailure: ${failure_num}\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m" +} + +function quick_format() { + if [[ $1 == "clang-format" ]]; then + clang_format + else + astyle_format + fi +} + +function do_astyle_check() { + [[ -z "$1" || ! -r "$1" ]] && return -1 + + do_astyle_fix "$1" | diff -pu --label="$1.orig" "$1" --label="$1" - + if [[ $? -ne 0 ]]; then + return -1 + fi +} + +function style_check() { +echo -e "\ +================================================================================================= + ███████╗████████╗██╗ ██╗██╗ ███████╗ ██████╗██╗ ██╗███████╗ ██████╗██╗ ██╗ + ██╔════╝╚══██╔══╝╚██╗ ██╔╝██║ ██╔════╝ ██╔════╝██║ ██║██╔════╝██╔════╝██║ ██╔╝ + ███████╗ ██║ ╚████╔╝ ██║ █████╗ ██║ ███████║█████╗ ██║ █████╔╝ + ╚════██║ ██║ ╚██╔╝ ██║ ██╔══╝ ██║ ██╔══██║██╔══╝ ██║ ██╔═██╗ + ███████║ ██║ ██║ ███████╗███████╗ ╚██████╗██║ ██║███████╗╚██████╗██║ ██╗ + ╚══════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝ ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═════╝╚═╝ ╚═╝ +=================================================================================================" + local start_time=$(date +%s) + local files + if [[ ${1} == "all" ]]; then + files=$(find ./src -regextype posix-extended -regex ".*\.(h|c|cc)") + else + files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$") + fi + files=(${files// / }) + local total=${#files[@]} + local failure_num=0 + local index=1 + for file in ${files[@]} + do + do_astyle_check ${file} + if [[ $? -ne 0 ]];then + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;31m\033[5m%s\033[0m\n" \ + ${index} ${total} ${file} "[FAILED]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + failure_num=$((failure_num+1)) + else + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;32m%-5s\033[0m\n" \ + ${index} ${total} ${file} "[PASS]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + fi + index=$((index+1)) + done + printf "%0.s=" {1..96} + printf "\n" + local end_time=$(date +%s) + local duration=$((${end_time} - ${start_time})) + echo -e "\033[1;36mTotal files: ${total}\033[0m, \033[1;32msuccess: $((total-failure_num))\033[0m, \033[1;31mfailure: ${failure_num}\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m" + if [[ ${failure_num} -ne 0 ]]; then + exit -1 + fi +} + +function cmetrics_check() { + echo -e "\ +=================================================================================================\033[1;36m + ______ __ ___ ______ ______ ____ ____ ______ _____ ______ __ __ ______ ______ __ __ + / ____// |/ // ____//_ __// __ \ / _// ____// ___/ / ____// / / // ____// ____// //_/ + / / / /|_/ // __/ / / / /_/ / / / / / \__ \ / / / /_/ // __/ / / / ,< + / /___ / / / // /___ / / / _, _/_/ / / /___ ___/ / / /___ / __ // /___ / /___ / /| | + \____//_/ /_//_____/ /_/ /_/ |_|/___/ \____/ /____/ \____//_/ /_//_____/ \____//_/ |_|\033[0m +=================================================================================================" + if [[ ${1} == "simple" ]]; then + printf "%0.s*" {1..97} + printf "\n" + result=$(python3 ${CMETRICS_TOOL} -fp ./src) + echo "${result}" + printf "%0.s*" {1..97} + printf "\n" + CyclomaticComplexityPerMethod=$(echo "${result}" | grep '\[\*\] Cyclomatic Complexity per Method' | awk '{print $NF}') + if [[ $(echo "${CyclomaticComplexityPerMethod} > 5" | bc) -eq 1 ]]; then + echo -e "\033[1;31mFailure: cyclomatic complexity per method(${CyclomaticComplexityPerMethod}) greater then 5\033[0m." + exit 1 + else + echo -e "\033[1;32mSuccess: cyclomatic complexity per method(${CyclomaticComplexityPerMethod}) less then 5\033[0m." + exit 0 + fi + fi + + local start_time=$(date +%s) + local files + if [[ ${1} == "all" ]]; then + files=$(find ./src -regextype posix-extended -regex ".*\.(h|c|cc)") + else + files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$") + fi + files=(${files// / }) + local total=${#files[@]} + local failure_num=0 + local index=1 + if [[ ${total} -eq 0 ]]; then + return 0 + fi + for file in ${files[@]} + do + result=$(python3 ${CMETRICS_TOOL} -fp ${file}) + CyclomaticComplexityperMethod=$(echo "${result}" | grep "\[\*\] Cyclomatic Complexity per Method:" | awk '{print $NF}') + CyclomaticComplexityperMethod=${CyclomaticComplexityperMethod:-0} + MaximumCyclomaticComplexity=$(echo "${result}" | grep "\[\*\] Maximum Cyclomatic Complexity:" | awk '{print $NF}') + MaximumCyclomaticComplexity=${MaximumCyclomaticComplexity:-0} + MaximumDepth=$(echo "${result}" | grep "\[\*\] Maximum Depth:" | awk '{print $NF}') + MaximumDepth=${MaximumDepth:-0} + RawLines=$(echo "${result}" | grep "\[\*\] Raw Lines:" | awk '{print $NF}') + RawLines=${RawLines:-0} + if [[ ${MaximumCyclomaticComplexity} -gt 10 ]] || [[ $(echo "${CyclomaticComplexityperMethod} > 5" | bc) -eq 1 ]] || \ + [[ ${MaximumDepth} -gt 5 ]] || [[ ${RawLines} -gt 2000 ]]; then + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;31m\033[5m%s\033[0m\n" \ + ${index} ${total} ${file} "[FAILED]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + failure_num=$((failure_num+1)) + else + printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;32m%-5s\033[0m\n" \ + ${index} ${total} ${file} "[PASS]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /' + fi + printf "%s\n%s\n%s\n %-1.3f | %3d | %2d | %4d\n%s\n" \ +"-------------------------------------------------------------------------------------------------" \ +" Cyclomatic Complexity per Method | Maximum Cyclomatic Complexity | Maximum Depth | Raw Lines " \ +"-------------------------------------------------------------------------------------------------" \ + ${CyclomaticComplexityperMethod} ${MaximumCyclomaticComplexity} ${MaximumDepth} ${RawLines} \ +"-------------------------------------------------------------------------------------------------" + index=$((index+1)) + done + printf "%0.s=" {1..97} + printf "\n" + local end_time=$(date +%s) + local duration=$((${end_time} - ${start_time})) + echo -e "\033[1;36mTotal files: ${total}\033[0m, \033[1;32msuccess: $((total-failure_num))\033[0m, \033[1;31mfailure: ${failure_num}\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m" + printf "%0.s*" {1..97} + printf "\n" + if [[ ${1} == "all" ]]; then + result=$(python3 ${CMETRICS_TOOL} -fp ./src) + else + result=$(python3 ${CMETRICS_TOOL} -fp ${files[@]}) + fi + echo "${result}" + printf "%0.s*" {1..96} + printf "\n" + CyclomaticComplexityPerMethod=$(echo "${result}" | grep '\[\*\] Cyclomatic Complexity per Method' | awk '{print $NF}') + if [[ $(echo "${CyclomaticComplexityPerMethod} > 5" | bc) -eq 1 ]]; then + echo -e "\033[1;31mFailure: cyclomatic complexity per method(${CyclomaticComplexityPerMethod}) greater then 5\033[0m." + exit 1 + else + echo -e "\033[1;32mSuccess: cyclomatic complexity per method(${CyclomaticComplexityPerMethod}) less then 5\033[0m." + exit 0 + fi +} + +function incremental_check() { + style_check "incremental" + if [[ $? -ne 0 ]]; then + exit -1 + fi + pclint_check "incremental" + if [[ $? -ne 0 ]]; then + exit -1 + fi + codestyle_check "incremental" + if [[ $? -ne 0 ]]; then + exit -1 + fi + cpp_check + if [[ $? -ne 0 ]]; then + return -1 + fi + + cmetrics_check "incremental" +} + +function static_check_all() { + style_check "all" + if [[ $? -ne 0 ]]; then + return -1 + fi + pclint_check "all" + if [[ $? -ne 0 ]]; then + return -1 + fi + codestyle_check "all" + if [[ $? -ne 0 ]]; then + return -1 + fi + cpp_check + if [[ $? -ne 0 ]]; then + return -1 + fi + + cmetrics_check "simple" +} + +if [[ $# -eq 0 ]];then + usage + exit 0 +fi + +args=`getopt -o upscmiaf:kh --long update-ci-tools,pclint,codestyle,detail-cmetrics,simple-cmetrics,incremental-check,all,quick-format:,style-check,cpp-check,help -- "$@"` +if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi +eval set -- "$args" + +while true; do + case "$1" in + -u|--update-ci-tools) update_ci_tools || (err "failed to update ci tools project" && exit -1); shift ;; + -p|--pclint) pclint_check "all" || (err "failed to perfrom pclint code static check" && exit -1); shift ;; + -s|--codestyle) codestyle_check "all" || (err "failed to perfrom codestyle(codedex) code static check" && exit -1); shift ;; + -c|--detail-cmetrics) cmetrics_check "all" || (err "failed to perform detail checks and statistics" && exit -1); shift ;; + -m|--simple-cmetrics) cmetrics_check "simple" || (err "failed to perform simple checks and statistics" && exit -1); shift ;; + -i|--incremental-check) incremental_check || (err "failed to perform incremental check" && exit -1); shift ;; + -a|--all) static_check_all || (err "failed to perform all checks and statistics" && exit -1); shift ;; + -f|--quick-format) quick_format $2 || (err "failed to format code" && exit -1); shift 2 ;; + -k|--style-check) style_check "all" || (err "failed to check code style" && exit -1); shift ;; + --cpp-check) cpp_check || (err "failed to check code style" && exit -1); shift ;; + -h|--help) usage ; exit 0 ;; + --) shift ; break ;; + *) err "invalid parameter" ; exit -1 ;; + esac +done + diff --git a/update-version.bash b/update-version.bash new file mode 100755 index 0000000..27924b5 --- /dev/null +++ b/update-version.bash @@ -0,0 +1,56 @@ +####################################################################### +##- @Copyright (C) Huawei Technologies., Ltd. 2017-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. +##- @Description: generate cetification +##- @Author: wujing +##- @Create: 2019-04-25 +####################################################################### +#!/bin/bash +# +# usage +# ./update-version.bash +topDir=$(git rev-parse --show-toplevel) +specfile="${topDir}/lcr.spec" +CMakefile="${topDir}/CMakeLists.txt" +old_version=$(cat ${specfile} | grep "%global" | grep "_version" | awk {'print $3'}) +first_old_version=$(cat ${specfile} | grep "%global" | grep "_version" | awk {'print $3'} | awk -F "." {'print $1'}) +second_old_version=$(cat ${specfile} | grep "%global" | grep "_version" | awk {'print $3'} | awk -F "." {'print $2'}) +third_old_version=$(cat ${specfile} | grep "%global" | grep "_version" | awk {'print $3'} | awk -F "." {'print $3'}) +read -p "Which level version do you want to upgrade?[1/2/3/d/N](default:N) select:" choice +if [[ ! -n "${choice}" || ${choice} == "N" ]]; then + echo "The version number has not been modified, it is still ${old_version}" + exit 0 +fi + +if [[ ${choice} -eq "1" ]]; then + first_old_version=$(($first_old_version+1)) + second_old_version="0" + third_old_version="0" +elif [[ ${choice} -eq "2" ]]; then + second_old_version=$(($second_old_version+1)) + third_old_version="0" +elif [[ ${choice} -eq "3" ]]; then + third_old_version=$(($third_old_version+1)) +fi + +new_version=${first_old_version}.${second_old_version}.${third_old_version} + +echo "The version number has been modified: ${old_version} => ${new_version}" + +old_release=$(cat ${specfile} | grep "%global" | grep "_release" | awk {'print $3'}) +commit_id_long=`git log --pretty=oneline -1 | awk {'print $1'}` +commit_id=${commit_id_long:0:8} +new_release=`date "+%Y%m%d"`.`date "+%H%M%S"`.git$commit_id +echo "The relase version has been modified, it is ${new_release}" +sed -i "s/set(LCR_VERSION \"${old_version}\")/set(LCR_VERSION \"${new_version}\")/g" ${CMakefile} +sed -i "s/^\%global _version ${old_version}$/\%global _version ${new_version}/g" ${specfile} +sed -i "s/^\%global _release ${old_release}$/\%global _release ${new_release}/g" ${specfile} + +echo "The release number has been modified: ${old_release} => ${new_release}"