提交 b4050921 编写于 作者: O overweight

Package init

上级
*.pyc
build
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)
木兰宽松许可证, 第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.
# 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.
# 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")
#cmakedefine VERSION "@LCR_VERSION@"
此差异已折叠。
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
%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 <hw.huiwang@huawei.com> - 0.0.1
- Initial RPM release
# 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)
/******************************************************************************
* 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 <stdio.h>
#include <string.h>
#include <securec.h>
#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;
}
/******************************************************************************
* 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 <stdlib.h>
#include <strings.h>
#include <stdarg.h>
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
# 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)
/******************************************************************************
* 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 <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#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);
}
/******************************************************************************
* 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 <stdbool.h>
#include <getopt.h>
#include <stdio.h>
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*/
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#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);
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#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;
}
/******************************************************************************
* 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 <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#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 */
/******************************************************************************
* 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 <stdio.h>
#include <string.h>
#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;
}
/******************************************************************************
* 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 <command>`
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 */
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#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=<dir|blkdev>";
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);
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#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);
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#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);
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdio.h>
#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;
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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);
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#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);
}
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#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);
}
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#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);
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#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);
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#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;
}
/******************************************************************************
* 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 */
/******************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
/******************************************************************************
* 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 */
此差异已折叠。
/******************************************************************************
* 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 */
此差异已折叠。
/******************************************************************************
* 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 */
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
# generate .c and .h files for json
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/schema)
此差异已折叠。
此差异已折叠。
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()
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
# get current directory sources files
message("-- do nothing ")
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册