提交 3b37e6bf 编写于 作者: D dogsheng

Package init

上级 7ceac727
......@@ -5,7 +5,7 @@ include(cmake/set_build_flags.cmake)
option(VERSION "set lcr version" ON)
if (VERSION STREQUAL "ON")
set(LCR_VERSION "1.0.15")
set(LCR_VERSION "1.0.17")
endif()
option(DEBUG "set lcr gcc option" ON)
......
......@@ -10,14 +10,6 @@ iSulad follows the kernel coding conventions. You can find a detailed introducti
- 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.
......@@ -6,7 +6,7 @@ includedir=@CMAKE_INSTALL_PREFIX@/include
Name: liblcr
Description: light-weighted container runtime library
Version: @LCR_VERSION@
URL: http://code.huawei.com/containers/liblcr
URL: liblcr
Libs: -L@CMAKE_INSTALL_PREFIX@/lib -llcr
Cflags: -I@CMAKE_INSTALL_PREFIX@/include
%global _version 1.0.15
%global _release 20190612.105034.gitefd3b7ac
%global _version 1.0.17
%global _release 20191222.223702.gita44996d6
Name: lcr
Version: %{_version}
Release: %{_release}
URL: http://code.huawei.com/containers/lcr
URL: lcr
Source: lcr-1.0.tar.gz
Summary: Lightweight Container Runtime
Group: Applications/System
......
......@@ -54,7 +54,6 @@ if (CMAKE_TOOLCHAIN_FILE)
target_link_libraries(liblcr ${EXTRAL_LINK_LIBS})
endif()
add_subdirectory(cmd)
if (LCR_GCOV)
target_link_libraries(liblcr -lgcov)
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 functions
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <securec.h>
#include "lcrcontainer.h"
#include "state.h"
#include "arguments.h"
#include "log.h"
#include "utils.h"
const char g_lcr_cmd_state_desc[] = "Output the state of a container";
static const char g_lcr_cmd_state_usage[] = "state --name=NAME";
struct lcr_arguments g_lcr_cmd_state_args;
static uint64_t read_memory_info(void)
{
uint64_t sysmem_limit = 0;
size_t len = 0;
char *line = NULL;
char *p = NULL;
FILE *fp = util_fopen("/proc/meminfo", "r");
if (fp == NULL) {
ERROR("Failed to open /proc/meminfo");
return sysmem_limit;
}
while (getline(&line, &len, fp) != -1) {
p = strchr(line, ' ');
if (p == NULL) {
goto out;
}
*p = '\0';
p++;
if (strcmp(line, "MemTotal:") == 0) {
while (*p != '\0' && (*p == ' ' || *p == '\t')) {
p++;
}
if (*p == '\0') {
goto out;
}
sysmem_limit = strtoull(p, NULL, 0);
break;
}
}
out:
fclose(fp);
free(line);
return sysmem_limit * SIZE_KB;
}
static void size_humanize(unsigned long long val, char *buf, size_t bufsz)
{
errno_t ret;
if (val > 1 << 30) {
ret = sprintf_s(buf, bufsz, "%u.%2.2u GiB",
(unsigned int)(val >> 30),
(unsigned int)((val & ((1 << 30) - 1)) * 100) >> 30);
} else if (val > 1 << 20) {
unsigned long long x = val + 5243; /* for rounding */
ret = sprintf_s(buf, bufsz, "%u.%2.2u MiB",
(unsigned int)(x >> 20), (unsigned int)(((x & ((1 << 20) - 1)) * 100) >> 20));
} else if (val > 1 << 10) {
unsigned long long x = val + 5; /* for rounding */
ret = sprintf_s(buf, bufsz, "%u.%2.2u KiB",
(unsigned int)(x >> 10), (unsigned int)(((x & ((1 << 10) - 1)) * 100) >> 10));
} else {
ret = sprintf_s(buf, bufsz, "%u bytes", (unsigned int)val);
}
if (ret < 0) {
ERROR("Failed to sprintf string");
return;
}
}
static void print_state(const struct lcr_container_state *lcs)
{
char buf[BUFSIZE];
fprintf(stdout, "%-15s %s\n", "Name:", lcs->name);
fprintf(stdout, "%-15s %s\n", "State:", lcs->state);
if (strcmp(lcs->state, "RUNNING") != 0) {
return;
}
fprintf(stdout, "%-15s %d\n", "PID:", lcs->init);
fprintf(stdout, "%-15s %.2f seconds\n", "CPU use:",
(double)lcs->cpu_use_nanos / 1000000000.0);
fprintf(stdout, "%-15s %llu\n", "Pids current:",
(unsigned long long)lcs->pids_current);
size_humanize(lcs->mem_used, buf, sizeof(buf));
fprintf(stdout, "%-15s %s\n", "Memory use:", buf);
size_humanize(lcs->mem_limit, buf, sizeof(buf));
fprintf(stdout, "%-15s %s\n", "Memory limit:", buf);
size_humanize(lcs->kmem_used, buf, sizeof(buf));
fprintf(stdout, "%-15s %s\n", "KMem use:", buf);
size_humanize(lcs->kmem_limit, buf, sizeof(buf));
fprintf(stdout, "%-15s %s\n", "KMem limit:", buf);
size_humanize(lcs->io_service_bytes.read, buf, sizeof(buf));
fprintf(stdout, "%-15s %s\n", "Blkio read:", buf);
size_humanize(lcs->io_service_bytes.write, buf, sizeof(buf));
fprintf(stdout, "%-15s %s\n", "Blkio write:", buf);
}
static inline int check_container_name()
{
if (g_lcr_cmd_state_args.name == NULL) {
fprintf(stderr, "missing --name,-n option\n");
return -1;
}
return 0;
}
static void set_sysmem_limit(struct lcr_container_state *state)
{
uint64_t sysmem_limit = 0;
sysmem_limit = read_memory_info();
if (sysmem_limit > 0 && state->mem_limit > sysmem_limit) {
state->mem_limit = sysmem_limit;
}
if (sysmem_limit > 0 && state->kmem_limit > sysmem_limit) {
state->kmem_limit = sysmem_limit;
}
}
int cmd_state_main(int argc, const char **argv)
{
struct lcr_container_state state = { 0 };
command_t cmd;
struct command_option options[] = {
STATE_OPTIONS(g_lcr_cmd_state_args),
COMMON_OPTIONS(g_lcr_cmd_state_args)
};
lcr_arguments_init(&g_lcr_cmd_state_args);
command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv,
g_lcr_cmd_state_desc, g_lcr_cmd_state_usage);
if (command_parse_args(&cmd, &g_lcr_cmd_state_args.argc, &g_lcr_cmd_state_args.argv)) {
exit(EINVALIDARGS);
}
if (lcr_log_init(g_lcr_cmd_state_args.name,
g_lcr_cmd_state_args.log_file,
g_lcr_cmd_state_args.log_priority,
g_lcr_cmd_state_args.progname,
g_lcr_cmd_state_args.quiet,
LOGPATH)) {
exit(EXIT_FAILURE);
}
if (check_container_name() != 0) {
exit(EXIT_FAILURE);
}
if (!lcr_state(g_lcr_cmd_state_args.name, g_lcr_cmd_state_args.lcrpath, &state)) {
fprintf(stderr, "Error get the container \"%s\"'s state\n", g_lcr_cmd_state_args.name);
exit(EXIT_FAILURE);
}
set_sysmem_limit(&state);
print_state(&state);
lcr_container_state_free(&state);
exit(EXIT_SUCCESS);
}
/******************************************************************************
* 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 functions
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <securec.h>
#include "lcrcontainer.h"
#include "update.h"
#include "arguments.h"
#include "log.h"
#include "utils.h"
#define BLKIOWEIGHT 1
#define CPUSHARES 2
#define CPUPERIOD 3
#define CPUQUOTA 4
#define CPUSETCPUS 5
#define CPUSETMEMS 6
#define KERNELMEMORY 7
#define MEMORYRESERV 8
#define MEMORYSWAP 9
const char g_lcr_cmd_update_desc[] = "Update configuration of a container";
static const char g_lcr_cmd_update_usage[] = "update --name=NAME";
struct lcr_arguments g_lcr_cmd_update_args;
static void to_cgroup_cpu_resources(const struct lcr_arguments *args, struct lcr_cgroup_resources *cr)
{
if (args->cr.blkio_weight) {
cr->blkio_weight = strtoull(args->cr.blkio_weight, NULL, 10);
}
if (args->cr.cpu_shares) {
cr->cpu_shares = strtoull(args->cr.cpu_shares, NULL, 10);
}
if (args->cr.cpu_period) {
cr->cpu_period = strtoull(args->cr.cpu_period, NULL, 10);
}
if (args->cr.cpu_quota) {
cr->cpu_quota = strtoull(args->cr.cpu_quota, NULL, 10);
}
if (args->cr.cpuset_cpus) {
cr->cpuset_cpus = args->cr.cpuset_cpus;
}
if (args->cr.cpuset_mems) {
cr->cpuset_mems = args->cr.cpuset_mems;
}
}
static void to_cgroup_mem_resources(const struct lcr_arguments *args, struct lcr_cgroup_resources *cr)
{
if (args->cr.kernel_memory_limit) {
cr->kernel_memory_limit = strtoull(args->cr.kernel_memory_limit, NULL, 10);
}
if (args->cr.memory_reservation) {
cr->memory_reservation = strtoull(args->cr.memory_reservation, NULL, 10);
}
if (args->cr.memory_limit) {
cr->memory_limit = strtoull(args->cr.memory_limit, NULL, 10);
}
if (args->cr.memory_swap) {
cr->memory_swap = strtoull(args->cr.memory_swap, NULL, 10);
}
}
static void to_cgroup_resources(const struct lcr_arguments *args, struct lcr_cgroup_resources *cr)
{
if (args == NULL || cr == NULL) {
return;
}
to_cgroup_cpu_resources(args, cr);
to_cgroup_mem_resources(args, cr);
}
int cmd_update_main(int argc, const char **argv)
{
command_t cmd;
struct lcr_cgroup_resources cr = { 0 };
struct command_option options[] = { UPDATE_OPTIONS(g_lcr_cmd_update_args), COMMON_OPTIONS(g_lcr_cmd_update_args) };
lcr_arguments_init(&g_lcr_cmd_update_args);
command_init(&cmd, options, sizeof(options) / sizeof(options[0]), argc, (const char **)argv, g_lcr_cmd_update_desc,
g_lcr_cmd_update_usage);
if (command_parse_args(&cmd, &g_lcr_cmd_update_args.argc, &g_lcr_cmd_update_args.argv)) {
exit(EINVALIDARGS);
}
if (lcr_log_init(g_lcr_cmd_update_args.name, g_lcr_cmd_update_args.log_file, g_lcr_cmd_update_args.log_priority,
g_lcr_cmd_update_args.progname, g_lcr_cmd_update_args.quiet, LOGPATH)) {
exit(EXIT_FAILURE);
}
if (g_lcr_cmd_update_args.name == NULL) {
fprintf(stderr, "missing --name,-n option\n");
exit(EXIT_FAILURE);
}
to_cgroup_resources(&g_lcr_cmd_update_args, &cr);
if (!lcr_update(g_lcr_cmd_update_args.name, g_lcr_cmd_update_args.lcrpath, &cr)) {
fprintf(stderr, "Error update container %s\n", g_lcr_cmd_update_args.name);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
/******************************************************************************
* 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 */
......@@ -2324,7 +2324,7 @@ static int append_seccomp_with_archs(const oci_runtime_config_linux_seccomp *sec
ret = -1;
goto out;
}
/* append rules*/
/* append rules */
for (j = 0; j < seccomp->syscalls_len; j++) {
if (seccomp_append_rules(seccomp->syscalls[j], buffer)) {
ret = -1;
......
......@@ -107,4 +107,4 @@ struct lcr_list *trans_annotations(const json_map_string_string *anno);
*/
struct lcr_list *get_needed_lxc_conf();
#endif /*__LCR_CONF_H*/
#endif /* __LCR_CONF_H */
......@@ -28,6 +28,8 @@
#define CONFIG_FILE_MODE 0640
#define NETWORK_MOUNT_FILE_MODE 0644
#define ARCH_LOG_FILE_MODE 0440
#define WORKING_LOG_FILE_MODE 0640
......
......@@ -24,7 +24,7 @@ typedef struct engine_error {
char *errmsg;
} engine_error_t;
/*record the lcr error*/
/* record the lcr error */
extern __thread engine_error_t g_lcr_error;
#define DEF_SUCCESS_STR "Success"
......
......@@ -44,13 +44,13 @@ extern "C" {
# undef linux
//options to report error if there is unknown key found in json
// options to report error if there is unknown key found in json
# define OPT_PARSE_STRICT 0x01
//options to generate all key and value
// options to generate all key and value
# define OPT_GEN_KAY_VALUE 0x02
//options to generate simplify(no indent) json string
// options to generate simplify(no indent) json string
# define OPT_GEN_SIMPLIFY 0x04
//options not to validate utf8 data
// options not to validate utf8 data
# define OPT_GEN_NO_VALIDATE_UTF8 0x08
# define GEN_SET_ERROR_AND_RETURN(stat, err) { \\
......
此差异已折叠。
......@@ -19,7 +19,7 @@ History: 2019-06-17
#!/usr/bin/python -Es
import helpers
def appendHeaderArray(obj, header, prefix):
def append_header_arr(obj, header, prefix):
'''
Description: Write c header file of array
Interface: None
......@@ -30,28 +30,29 @@ def appendHeaderArray(obj, header, prefix):
header.write("typedef struct {\n")
for i in obj.subtypobj:
if i.typ == 'array':
c_typ = helpers.getPrefixPointer(i.name, i.subtyp, prefix) or \
helpers.getMapCTypes(i.subtyp)
c_typ = helpers.get_prefixe_pointer(i.name, i.subtyp, prefix) or \
helpers.get_map_c_types(i.subtyp)
if i.subtypobj is not None:
c_typ = helpers.getNameSubstr(i.name, prefix)
c_typ = helpers.get_name_substr(i.name, prefix)
if not helpers.judgeComplex(i.subtyp):
if not helpers.judge_complex(i.subtyp):
header.write(" %s%s*%s;\n" % (c_typ, " " if '*' not in c_typ else "", \
i.fixname))
else:
header.write(" %s **%s;\n" % (c_typ, i.fixname))
header.write(" size_t %s;\n\n" % (i.fixname + "_len"))
else:
c_typ = helpers.getPrefixPointer(i.name, i.typ, prefix) or \
helpers.getMapCTypes(i.typ)
c_typ = helpers.get_prefixe_pointer(i.name, i.typ, prefix) or \
helpers.get_map_c_types(i.typ)
header.write(" %s%s%s;\n" % (c_typ, " " if '*' not in c_typ else "", i.fixname))
typename = helpers.getNameSubstr(obj.name, prefix)
typename = helpers.get_name_substr(obj.name, prefix)
header.write("}\n%s;\n\n" % typename)
header.write("void free_%s(%s *ptr);\n\n" % (typename, typename))
header.write("%s *make_%s(yajl_val tree, const struct parser_context *ctx, parser_error *err);"\
"\n\n" % (typename, typename))
def appendHeaderMapStrObj(obj, header, prefix):
def append_header_map_str_obj(obj, header, prefix):
'''
Description: Write c header file of mapStringObject
Interface: None
......@@ -60,86 +61,89 @@ def appendHeaderMapStrObj(obj, header, prefix):
child = obj.children[0]
header.write("typedef struct {\n")
header.write(" char **keys;\n")
if helpers.validBasicMapName(child.typ):
c_typ = helpers.getPrefixPointer("", child.typ, "")
if helpers.valid_basic_map_name(child.typ):
c_typ = helpers.get_prefixe_pointer("", child.typ, "")
elif child.subtypname:
c_typ = child.subtypname
else:
c_typ = helpers.getPrefixPointer(child.name, child.typ, prefix)
c_typ = helpers.get_prefixe_pointer(child.name, child.typ, prefix)
header.write(" %s%s*%s;\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname))
header.write(" size_t len;\n")
def appendHeaderChildArray(child, header, prefix):
def append_header_child_arr(child, header, prefix):
'''
Description: Write c header file of array of child
Interface: None
History: 2019-06-17
'''
if helpers.getMapCTypes(child.subtyp) != "":
c_typ = helpers.getMapCTypes(child.subtyp)
elif helpers.validBasicMapName(child.subtyp):
c_typ = '%s *' % helpers.makeBasicMapName(child.subtyp)
if helpers.get_map_c_types(child.subtyp) != "":
c_typ = helpers.get_map_c_types(child.subtyp)
elif helpers.valid_basic_map_name(child.subtyp):
c_typ = '%s *' % helpers.make_basic_map_name(child.subtyp)
elif child.subtypname is not None:
c_typ = child.subtypname
elif child.subtypobj is not None:
c_typ = helpers.getNameSubstr(child.name, prefix)
c_typ = helpers.get_name_substr(child.name, prefix)
else:
c_typ = helpers.getPrefixPointer(child.name, child.subtyp, prefix)
c_typ = helpers.get_prefixe_pointer(child.name, child.subtyp, prefix)
if helpers.validBasicMapName(child.subtyp):
header.write(" %s **%s;\n" % (helpers.makeBasicMapName(child.subtyp), child.fixname))
elif not helpers.judgeComplex(child.subtyp):
if helpers.valid_basic_map_name(child.subtyp):
header.write(" %s **%s;\n" % (helpers.make_basic_map_name(child.subtyp), child.fixname))
elif not helpers.judge_complex(child.subtyp):
header.write(" %s%s*%s;\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname))
else:
header.write(" %s%s**%s;\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname))
header.write(" size_t %s;\n\n" % (child.fixname + "_len"))
def appendHeaderChildOthers(child, header, prefix):
def append_header_child_others(child, header, prefix):
'''
Description: Write c header file of others of child
Interface: None
History: 2019-06-17
'''
if helpers.getMapCTypes(child.typ) != "":
c_typ = helpers.getMapCTypes(child.typ)
elif helpers.validBasicMapName(child.typ):
c_typ = '%s *' % helpers.makeBasicMapName(child.typ)
if helpers.get_map_c_types(child.typ) != "":
c_typ = helpers.get_map_c_types(child.typ)
elif helpers.valid_basic_map_name(child.typ):
c_typ = '%s *' % helpers.make_basic_map_name(child.typ)
elif child.subtypname:
c_typ = helpers.getPrefixPointer(child.subtypname, child.typ, "")
c_typ = helpers.get_prefixe_pointer(child.subtypname, child.typ, "")
else:
c_typ = helpers.getPrefixPointer(child.name, child.typ, prefix)
c_typ = helpers.get_prefixe_pointer(child.name, child.typ, prefix)
header.write(" %s%s%s;\n\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname))
def appendTypeCHeader(obj, header, prefix):
def append_type_c_header(obj, header, prefix):
'''
Description: Write c header file
Interface: None
History: 2019-06-17
'''
if not helpers.judgeComplex(obj.typ):
if not helpers.judge_complex(obj.typ):
return
if obj.typ == 'array':
appendHeaderArray(obj, header, prefix)
append_header_arr(obj, header, prefix)
return
if obj.typ == 'mapStringObject':
if obj.subtypname is not None:
return
appendHeaderMapStrObj(obj, header, prefix)
append_header_map_str_obj(obj, header, prefix)
elif obj.typ == 'object':
if obj.subtypname is not None:
return
header.write("typedef struct {\n")
if obj.children is None:
header.write(" char unuseful;//unuseful definition to avoid empty struct\n")
for i in obj.children or [ ]:
header.write(" char unuseful; // unuseful definition to avoid empty struct\n")
for i in obj.children or []:
if i.typ == 'array':
appendHeaderChildArray(i, header, prefix)
append_header_child_arr(i, header, prefix)
else:
appendHeaderChildOthers(i, header, prefix)
append_header_child_others(i, header, prefix)
typename = helpers.getPrefixName(obj.name, prefix)
typename = helpers.get_prefixe_name(obj.name, prefix)
header.write("}\n%s;\n\n" % typename)
header.write("void free_%s(%s *ptr);\n\n" % (typename, typename))
header.write("%s *make_%s(yajl_val tree, const struct parser_context *ctx, parser_error *err)"\
......@@ -147,7 +151,8 @@ def appendTypeCHeader(obj, header, prefix):
header.write("yajl_gen_status gen_%s(yajl_gen g, const %s *ptr, const struct parser_context "\
"*ctx, parser_error *err);\n\n" % (typename, typename))
def headerReflection(structs, schema_info, header):
def header_reflect(structs, schema_info, header):
'''
Description: Reflection header files
Interface: None
......@@ -168,7 +173,7 @@ def headerReflection(structs, schema_info, header):
header.write("#endif\n\n")
for i in structs:
appendTypeCHeader(i, header, prefix)
append_type_c_header(i, header, prefix)
length = len(structs)
toptype = structs[length - 1].typ if length != 0 else ""
if toptype == 'object':
......@@ -195,3 +200,4 @@ def headerReflection(structs, schema_info, header):
header.write("}\n")
header.write("#endif\n\n")
header.write("#endif\n\n")
......@@ -20,16 +20,16 @@ History: 2019-06-17
import os
import sys
def appendSeparator(subStr):
def append_separator(substr):
'''
Description: append only '_' at last position of subStr
Interface: None
History: 2019-09-20
'''
if subStr and subStr[-1] != '_':
subStr.append('_')
if substr and substr[-1] != '_':
substr.append('_')
def convertToCStyle(name):
def conv_to_c_style(name):
'''
Description: convert name to linux c format
Interface: None
......@@ -38,33 +38,33 @@ def convertToCStyle(name):
if name is None or name == "":
return ""
name = name.replace('.', '_').replace('-', '_').replace('/', '_')
subStr = []
substr = []
preindex = 0
index = 0
for index, char in enumerate(name):
if char == '_':
appendSeparator(subStr)
subStr.append(name[preindex:index].lower())
append_separator(substr)
substr.append(name[preindex:index].lower())
preindex = index + 1
if not char.isupper() and name[preindex].isupper() and \
name[preindex + 1].isupper():
appendSeparator(subStr)
subStr.append(name[preindex:index - 1].lower())
append_separator(substr)
substr.append(name[preindex:index - 1].lower())
preindex = index - 1
continue
if char.isupper() and index > 0 and name[index - 1].islower():
appendSeparator(subStr)
subStr.append(name[preindex:index].lower())
append_separator(substr)
substr.append(name[preindex:index].lower())
preindex = index
if preindex <= index and index >= 0 and name[index] != '_' and \
preindex != 0:
appendSeparator(subStr)
subStr.append(name[preindex:index + 1].lower())
result = ''.join(subStr)
append_separator(substr)
substr.append(name[preindex:index + 1].lower())
result = ''.join(substr)
return result
def getMapCTypes(typ):
def get_map_c_types(typ):
'''
Description: Get map c types
Interface: None
......@@ -103,7 +103,7 @@ def getMapCTypes(typ):
return map_c_types[typ]
return ""
def validBasicMapName(typ):
def valid_basic_map_name(typ):
'''
Description: Valid basic map name
Interface: None
......@@ -112,14 +112,14 @@ def validBasicMapName(typ):
return typ != 'mapStringObject' and hasattr(typ, 'startswith') and \
typ.startswith('map')
def makeBasicMapName(mapname):
def make_basic_map_name(mapname):
'''
Description: Make basic map name
Interface: None
History: 2019-06-17
'''
basic_map_types = ('string', 'int', 'bool')
parts = convertToCStyle(mapname).split('_')
parts = conv_to_c_style(mapname).split('_')
if len(parts) != 3 or parts[0] != 'map' or \
(parts[1] not in basic_map_types) or \
(parts[2] not in basic_map_types):
......@@ -128,7 +128,7 @@ def makeBasicMapName(mapname):
return "json_map_%s_%s" % (parts[1], parts[2])
def getNameSubstr(name, prefix):
def get_name_substr(name, prefix):
'''
Description: Make array name
Interface: None
......@@ -137,7 +137,7 @@ def getNameSubstr(name, prefix):
return "%s_element" % prefix if name is None or name == "" or prefix == name \
else "%s_%s_element" % (prefix, name)
def getPrefixName(name, prefix):
def get_prefixe_name(name, prefix):
'''
Description: Make name
Interface: None
......@@ -149,19 +149,19 @@ def getPrefixName(name, prefix):
return "%s" % name
return "%s_%s" % (prefix, name)
def getPrefixPointer(name, typ, prefix):
def get_prefixe_pointer(name, typ, prefix):
'''
Description: Make pointer name
Interface: None
History: 2019-06-17
'''
if typ != 'object' and typ != 'mapStringObject' and \
not validBasicMapName(typ):
not valid_basic_map_name(typ):
return ""
return '%s *' % makeBasicMapName(typ) if validBasicMapName(typ) \
else "%s *" % getPrefixName(name, prefix)
return '%s *' % make_basic_map_name(typ) if valid_basic_map_name(typ) \
else "%s *" % get_prefixe_name(name, prefix)
def judgeComplex(typ):
def judge_complex(typ):
'''
Description: Check compound object
Interface: None
......@@ -169,7 +169,7 @@ def judgeComplex(typ):
'''
return typ in ('object', 'array', 'mapStringObject')
def judgeDataType(typ):
def judge_data_type(typ):
'''
Description: Check numeric type
Interface: None
......@@ -180,7 +180,7 @@ def judgeDataType(typ):
return True
return typ in ("integer", "UID", "GID", "double")
def judgeDataPointerType(typ):
def judge_data_pointer_type(typ):
'''
Description: Check numeric pointer type
Interface: None
......@@ -190,7 +190,7 @@ def judgeDataPointerType(typ):
return True
return False
def obtainDataPointerType(typ):
def obtain_data_pointer_type(typ):
'''
Description: Get numeric pointer type
Interface: None
......@@ -199,22 +199,22 @@ def obtainDataPointerType(typ):
index = typ.find("Pointer")
return typ[0:index] if index != -1 else ""
def obtainPointer(name, typ, prefix):
def obtain_pointer(name, typ, prefix):
'''
Description: Obtain pointer string
Interface: None
History: 2019-06-17
'''
ptr = getPrefixPointer(name, typ, prefix)
ptr = get_prefixe_pointer(name, typ, prefix)
if ptr != "":
return ptr
return "char *" if typ == "string" else \
("%s *" % typ if typ == "ArrayOfStrings" else "")
class CombinationName(object):
class CombinateName(object):
'''
Description: Store CombinationName information
Description: Store CombinateName information
Interface: None
History: 2019-06-17
'''
......@@ -236,7 +236,7 @@ class CombinationName(object):
History: 2019-06-17
'''
prefix_name = self.name + '_' if self.name != "" else ""
return CombinationName(prefix_name + leaf, leaf)
return CombinateName(prefix_name + leaf, leaf)
class Unite(object):
......@@ -253,9 +253,9 @@ class Unite(object):
self.subtypobj = subtypobj
self.subtypname = subtypname
self.required = required
self.name = convertToCStyle(name.name.replace('.', '_'))
self.name = conv_to_c_style(name.name.replace('.', '_'))
self.origname = name.leaf or name.name
self.fixname = convertToCStyle(self.origname.replace('.', '_'))
self.fixname = conv_to_c_style(self.origname.replace('.', '_'))
......@@ -297,7 +297,7 @@ class SchemaInfo(object):
def __init__(self, name, header, source, prefix, filesdir, refs=None):
self.name = name
self.fileprefix = convertToCStyle( \
self.fileprefix = conv_to_c_style( \
name.basename.replace('.', '_').replace('-', '_'))
self.header = header
self.source = source
......@@ -311,3 +311,7 @@ class SchemaInfo(object):
def __str__(self):
return self.__repr__(self)
此差异已折叠。
......@@ -22,7 +22,7 @@ struct lcr_list {
struct lcr_list *prev;
};
/* Iterate through an lcr list.*/
/* Iterate through an lcr list. */
#define lcr_list_for_each(__iterator, __list) \
for ((__iterator) = (__list)->next; \
(__iterator) != (__list); \
......
此差异已折叠。
......@@ -42,11 +42,11 @@ struct lcr_console_config {
* Store lcr container info
*/
struct lcr_container_info {
/* Name of container.*/
/* Name of container. */
char *name;
/* State of container.*/
/* State of container. */
char *state;
/* Interface of container.*/
/* Interface of container. */
char *interface;
char *ipv4;
char *ipv6;
......@@ -70,15 +70,15 @@ struct lcr_container_state {
char *name;
/* State of container */
char *state;
/* The process ID of the init container*/
/* The process ID of the init container */
pid_t init;
/* Current pids */
uint64_t pids_current;
/* CPU usage*/
/* CPU usage */
uint64_t cpu_use_nanos;
uint64_t cpu_use_user;
uint64_t cpu_use_sys;
/* BlkIO usage*/
/* BlkIO usage */
struct blkio_stats io_service_bytes;
struct blkio_stats io_serviced;
/* Memory usage */
......@@ -142,12 +142,9 @@ void lcr_containers_info_free(struct lcr_container_info **info_arr, size_t size)
* param name : container name
* param lcrpath: container path
* param rootfs : the path of rootfs used for the container
* param dist : the distribution for generate specification
* NOTE: if the dist is `none`, it will not create default spec
* param oci_config_data : json string of oci config data
*/
bool lcr_create(const char *name, const char *lcrpath, const char *rootfs,
const char *dist, const void *oci_config_data);
bool lcr_create(const char *name, const char *lcrpath, const char *rootfs, const void *oci_config_data);
/*
* Start a container
......@@ -179,9 +176,7 @@ struct lcr_start_request {
bool daemonize;
bool tty;
bool open_stdin;
const char *pidfile;
const char **console_fifos;
const char *console_logpath;
uint32_t start_timeout;
const char *container_pidfile;
const char *exit_fifo;
......@@ -190,8 +185,6 @@ struct lcr_start_request {
gid_t gid;
gid_t *additional_gids;
size_t additional_gids_len;
const char **share_ns;
};
bool lcr_start(const struct lcr_start_request *request);
......@@ -211,17 +204,6 @@ bool lcr_kill(const char *name, const char *lcrpath, uint32_t signal);
*/
bool lcr_delete(const char *name, const char *lcrpath);
/*
* Execute process inside a container
* param name : container name, required.
* param lcrpath : container path, set to NULL if you want use default lcrpath.
* param argc : the size of the argv array
* param argv : array of arguments to be execute inside container
* param pid : ID of process running inside container
*/
bool lcr_exec(const char *name, const char *lcrpath, int argc, char * const * argv, pid_t *pid);
bool lcr_clean(const char *name, const char *lcrpath, const char *logpath, const char *loglevel, pid_t pid);
/*
......@@ -275,26 +257,28 @@ void lcr_free_console_config(struct lcr_console_config *config);
int lcr_log_init(const char *name, const char *file, const char *priority,
const char *prefix, int quiet, const char *lcrpath);
struct lcr_exec_request {
const char *name;
const char *lcrpath;
const char *logpath;
const char *loglevel;
const char **console_fifos;
const char *user;
const char **env;
size_t env_len;
const char **args;
size_t args_len;
int64_t timeout;
};
/*
* Execute process inside a container
* param name : container name, required.
* param lcrpath : container path, set to NULL if you want use default lcrpath.
* param stdinfd : stdinfd
* param stdoutfd : stdoutfd
* param stderrfd : stderrfd
* param escape : prefix for escape command, for example if escape equals 1 means the escape
* prefix is <CTRL+a>, you can type <CTRL+a q> to exit the console.
* -1 means never exit console by hand.
* param argc : the size of the argv array
* param argv : array of arguments to be execute inside container(terminated with "NULL")
* param env : array of environment variables to be set inside container(terminated with "NULL")
* param timeout : Timeout in seconds for attach container
* param pid : ID of process running inside container
* param exit_code : exit_code of process running inside container
*/
bool lcr_attach(const char *name, const char *lcrpath, const char *logpath, const char *loglevel,
const char *console_fifos[], char * const argv[], char * const env[],
int64_t timeout, pid_t *pid, int *exit_code);
bool lcr_exec(const struct lcr_exec_request *request, int *exit_code);
bool lcr_update(const char *name, const char *lcrpath, const struct lcr_cgroup_resources *cr);
......
此差异已折叠。
......@@ -25,14 +25,9 @@ bool do_update(struct lxc_container *c, const char *name, const char *lcrpath, c
void do_lcr_state(struct lxc_container *c, struct lcr_container_state *lcs);
bool do_attach(const char *name, const char *path, const char *logpath, const char *loglevel,
const char *console_fifos[], const char * const argv[], const char * const env[], int64_t timeout,
pid_t *exec_pid, int *exit_code);
void execute_lxc_start(const char *name, const char *path, const char *logpath, const char *loglevel,
bool daemonize, bool tty, bool open_stdin, const char *pidfile,
const char *console_fifos[], const char *console_logpath, const char *share_ns[],
uint32_t start_timeout, const char *container_pidfile, const char *exit_fifo);
bool do_attach(const char *name, const char *path, const struct lcr_exec_request *request, int *exit_code);
void execute_lxc_start(const char *name, const char *path, const struct lcr_start_request *request);
#ifdef __cplusplus
}
......
此差异已折叠。
......@@ -69,14 +69,6 @@ struct lcr_list *lcr_oci2lcr(const struct lxc_container *c, const char *containe
oci_runtime_spec *new_container,
char **seccomp);
/*
* Generate lcr_config_info according to distribution.
* param distribution : system distribution, now support: ubuntu
* param seccomp_conf [out] : return seccomp configuration if needed.
* return: a linked list
*/
struct lcr_list *lcr_dist2spec(const char *distribution, char **seccomp_conf);
/*
* Create a new specification file
* param name : container name, required.
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册