sync openeuler

Signed-off-by: Grooooot <isula@huawei.com>
This commit is contained in:
Grooooot 2020-01-20 10:43:34 +08:00
parent 12c68c03d4
commit b5c23f3097
18 changed files with 378 additions and 448 deletions

View File

@ -39,7 +39,6 @@ execute_process(
)
message("-- commit id: " ${GIT_COMMIT_HASH})
set(CMAKE_C_FLAGS "-fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie -shared -pthread")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie")

View File

@ -4,6 +4,9 @@ set(CMAKE_CXX_FLAGS "-fPIC -std=c++11 -fstack-protector-all -D_FORTIFY_SOURCE=2
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie -shared -pthread")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
if (LCR_GCOV)
set(CMAKE_C_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage")

View File

@ -1,5 +1,5 @@
%global _version 1.0.18
%global _release 20200105.223545.git6259bd3e
%global _release 20200120.104405.git2310fafe
Name: lcr
Version: %{_version}
Release: %{_release}

View File

@ -38,13 +38,6 @@ target_include_directories(liblcr
# set liblcr compile flags
set_target_properties(liblcr PROPERTIES PREFIX "")
if (LCR_GCOV)
set(CMAKE_C_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage")
message("------compile with gcov-------------")
message("-----CFLAGS: " ${CMAKE_C_FLAGS_DEBUG})
message("------------------------------------")
endif()
target_link_libraries(liblcr ${check_libs})
if (CMAKE_TOOLCHAIN_FILE)

View File

@ -12,8 +12,8 @@
* Create: 2018-11-08
* Description: provide container buffer definition
******************************************************************************/
#ifndef LCRD_BUFFER_H
#define LCRD_BUFFER_H
#ifndef LCR_BUFFER_H
#define LCR_BUFFER_H
#include <stdlib.h>
#include <strings.h>

View File

@ -13,8 +13,8 @@
* Description: provide constant definition
******************************************************************************/
#ifndef _LCRD_CONSTANTS_H
#define _LCRD_CONSTANTS_H
#ifndef _LCR_CONSTANTS_H
#define _LCR_CONSTANTS_H
/* mode of file and directory */

View File

@ -4,19 +4,24 @@ Description: commom source file
Interface: None
History: 2019-06-17
'''
# - Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
# - lcr licensed under the Mulan PSL v1.
# - You can use this software according to the terms and conditions of the Mulan PSL v1.
# - You may obtain a copy of Mulan PSL v1 at:
# - http://license.coscl.org.cn/MulanPSL
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v1 for more details.
# - Description: generate json
# - Author: tanyifeng
# - Create: 2018-04-25
#!/usr/bin/python -Es
#
# libocispec - a C library for parsing OCI spec files.
#
# Copyright (C) 2017, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
# Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
#
# libocispec is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# libocispec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libocispec. If not, see <http://www.gnu.org/licenses/>.
CODE = '''// Auto generated file. Do not edit!
# define _GNU_SOURCE

View File

@ -4,18 +4,25 @@ Description: commom header file
Interface: None
History: 2019-06-17
'''
# - Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
# - lcr licensed under the Mulan PSL v1.
# - You can use this software according to the terms and conditions of the Mulan PSL v1.
# - You may obtain a copy of Mulan PSL v1 at:
# - http://license.coscl.org.cn/MulanPSL
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v1 for more details.
# - Description: generate json
# - Author: tanyifeng
# - Create: 2018-04-25
#
# libocispec - a C library for parsing OCI spec files.
#
# Copyright (C) 2017, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
# Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
#
# libocispec is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# libocispec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libocispec. If not, see <http://www.gnu.org/licenses/>.
#
#!/usr/bin/python -Es
"""

View File

@ -4,19 +4,24 @@ Description: header class and functions
Interface: None
History: 2019-06-17
'''
# - Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
# - lcr licensed under the Mulan PSL v1.
# - You can use this software according to the terms and conditions of the Mulan PSL v1.
# - You may obtain a copy of Mulan PSL v1 at:
# - http://license.coscl.org.cn/MulanPSL
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v1 for more details.
# - Description: generate json
# - Author: tanyifeng
# - Create: 2018-04-25
#!/usr/bin/python -Es
# libocispec - a C library for parsing OCI spec files.
#
# Copyright (C) 2017, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
# Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
#
# libocispec is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# libocispec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libocispec. If not, see <http://www.gnu.org/licenses/>.
import traceback
import os

View File

@ -4,18 +4,25 @@ Description: header class and functions
Interface: None
History: 2019-06-17
'''
# - Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
# - lcr licensed under the Mulan PSL v1.
# - You can use this software according to the terms and conditions of the Mulan PSL v1.
# - You may obtain a copy of Mulan PSL v1 at:
# - http://license.coscl.org.cn/MulanPSL
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v1 for more details.
# - Description: generate json
# - Author: tanyifeng
# - Create: 2018-04-25
#
# libocispec - a C library for parsing OCI spec files.
#
# Copyright (C) 2017, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
# Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
#
# libocispec is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# libocispec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libocispec. If not, see <http://www.gnu.org/licenses/>.
#
#!/usr/bin/python -Es
import helpers

View File

@ -4,18 +4,25 @@ Description: helper class and functions
Interface: None
History: 2019-06-17
'''
# - Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
# - lcr licensed under the Mulan PSL v1.
# - You can use this software according to the terms and conditions of the Mulan PSL v1.
# - You may obtain a copy of Mulan PSL v1 at:
# - http://license.coscl.org.cn/MulanPSL
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v1 for more details.
# - Description: generate json
# - Author: tanyifeng
# - Create: 2018-04-25
#
# libocispec - a C library for parsing OCI spec files.
#
# Copyright (C) 2017, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
# Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
#
# libocispec is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# libocispec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libocispec. If not, see <http://www.gnu.org/licenses/>.
#
#!/usr/bin/python -Es
import os
import sys

View File

@ -1,17 +1,17 @@
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
* lcr licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: tanyifeng
* Create: 2018-11-1
* Description: provide file read functions
********************************************************************************/
/*
Copyright 2017 Giuseppe Scrivano
Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

View File

@ -1,17 +1,17 @@
/*****************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
* lcr licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: tanyifeng
* Create: 2018-11-08
* Description: provide container read file definition
****************************************************************************/
/*
Copyright 2017 Giuseppe Scrivano
Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __JSON_READ_FILE_H_
#define __JSON_READ_FILE_H_

View File

@ -1,21 +1,24 @@
# -*- coding: utf-8 -*-
"""
Copyright (C) Huawei Technologies., Ltd. 2017-2019. All rights reserved.
# - lcr licensed under the Mulan PSL v1.
# - You can use this software according to the terms and conditions of the Mulan PSL v1.
# - You may obtain a copy of Mulan PSL v1 at:
# - http://license.coscl.org.cn/MulanPSL
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v1 for more details.
Description: generate json
Author: tanyifeng
Interface: None
History: 2018-04-25 created
2019-06-17 Code specification
"""
#!/usr/bin/python -Es
#
# libocispec - a C library for parsing OCI spec files.
#
# Copyright (C) 2017, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
# Copyright (C) Huawei Technologies., Ltd. 2018-2019. All rights reserved.
#
# libocispec is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# libocispec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libocispec. If not, see <http://www.gnu.org/licenses/>.
#
import helpers

View File

@ -1968,7 +1968,7 @@ int lcr_log_init(const char *name, const char *file, const char *priority, const
lconf.driver = "fifo";
lconf.priority = priority;
}
if (log_enable(&lconf)) {
if (engine_log_enable(&lconf)) {
fprintf(stderr, "Failed to init log");
goto out;
}

View File

@ -154,7 +154,7 @@ bool lcr_create(const char *name, const char *lcrpath, const char *rootfs, const
* param loglevel : log level.
* param pidfile : container pidfile path, set to NULL if you don't need.
* param daemonize : daemonize the container.
* console_fifos[] : path of the console fifos,[0]:input, [1]:output.used internal by lcrd
* console_fifos[] : path of the console fifos,[0]:input, [1]:output.used internal by iSulad
* console_logpath :path of console log file,
* set to NULL if want to use the default configure(base on the config file)
set to PATH(for example "/home/XX/XX.log"), LXC will save the console to this file

390
src/log.c
View File

@ -1,6 +1,6 @@
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
* lcr licensed under the Mulan PSL v1.
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* engine 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
@ -9,11 +9,12 @@
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: wujing
* Create: 2018-11-08
* Create: 2019-4-08
* Description: provide container log functions
******************************************************************************/
#define _GNU_SOURCE
#define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
@ -24,27 +25,13 @@
#include <errno.h>
#include <unistd.h>
#include <inttypes.h>
#include <time.h>
#include "log.h"
#include "utils.h"
#include "log.h"
const char * const g_engine_log_prio_name[] = {
"FATAL", "ALERT", "CRIT", "ERROR", "WARN",
"NOTICE", "INFO", "DEBUG", "TRACE"
};
/* predefined priorities. */
enum engine_log_priority {
LOG_PRIORITY_FATAL = LOG_EMERG,
LOG_PRIORITY_ALERT = LOG_ALERT,
LOG_PRIORITY_CRIT = LOG_CRIT,
LOG_PRIORITY_ERROR = LOG_ERR,
LOG_PRIORITY_WARN = LOG_WARNING,
LOG_PRIORITY_NOTICE = LOG_NOTICE,
LOG_PRIORITY_INFO = LOG_INFO,
LOG_PRIORITY_DEBUG = LOG_DEBUG,
LOG_PRIORITY_TRACE,
LOG_PRIORITY_MAX
"FATAL", "ALERT", "CRIT", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG", "TRACE"
};
#define MAX_MSG_LENGTH 4096
@ -52,9 +39,8 @@ enum engine_log_priority {
static __thread char *g_engine_log_prefix = NULL;
static char *g_engine_log_vmname = NULL;
static bool g_engine_log_quiet = false;
static int g_engine_log_level = LOG_PRIORITY_DEBUG;
static char *g_engine_log_module = NULL;
static int g_engine_log_level = ENGINE_LOG_DEBUG;
static int g_engine_log_driver = LOG_DRIVER_STDOUT;
int g_engine_log_fd = -1;
@ -66,34 +52,34 @@ void engine_set_log_prefix(const char *prefix)
}
free(g_engine_log_prefix);
g_engine_log_prefix = util_strdup_s(prefix);
}
/* engine free log prefix */
void engine_free_log_prefix()
void engine_free_log_prefix(void)
{
free(g_engine_log_prefix);
g_engine_log_prefix = NULL;
}
ssize_t write_nointr(int fd, const void *buf, size_t count);
static ssize_t write_nointr(int fd, const void *buf, size_t count);
void log_append_logfile(const struct engine_log_event *event, const char *timestamp, const char *msg);
void log_append_stderr(const struct engine_log_event *event, const char *timestamp, const char *msg);
int engine_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *time);
static void do_fifo_log(const struct engine_log_object_metadata *metadata, const char *timestamp,
const char *msg);
static void do_stderr_log(const struct engine_log_object_metadata *metadata, const char *timestamp,
const char *msg);
/* engine change str logdriver to enum */
int engine_change_str_logdriver_to_enum(const char *driver)
static int engine_change_str_logdriver_to_enum(const char *driver)
{
if (driver == NULL) {
return LOG_DRIVER_NOSET;
}
if (!strcasecmp(driver, "stdout")) {
if (strcasecmp(driver, "stdout") == 0) {
return LOG_DRIVER_STDOUT;
}
if (!strcasecmp(driver, "fifo")) {
if (strcasecmp(driver, "fifo") == 0) {
return LOG_DRIVER_FIFO;
}
@ -103,25 +89,25 @@ int engine_change_str_logdriver_to_enum(const char *driver)
#define LOG_FIFO_SIZE (1024 * 1024)
/* open fifo */
int open_fifo(const char *fifo_path)
static int open_fifo(const char *fifo_path)
{
int nret;
int fifo_fd = -1;
nret = mknod(fifo_path, S_IFIFO | S_IRUSR | S_IWUSR, (dev_t)0);
if (nret && errno != EEXIST) {
printf("Mknod failed: %s\n", strerror(errno));
COMMAND_ERROR("Mknod failed: %s", strerror(errno));
return nret;
}
fifo_fd = util_open(fifo_path, O_RDWR | O_NONBLOCK, 0);
if (fifo_fd == -1) {
fprintf(stderr, "Open fifo %s failed: %s\n", fifo_path, strerror(errno));
COMMAND_ERROR("Open fifo %s failed: %s", fifo_path, strerror(errno));
return -1;
}
if (fcntl(fifo_fd, F_SETPIPE_SZ, LOG_FIFO_SIZE) == -1) {
fprintf(stderr, "Set fifo buffer size failed: %s", strerror(errno));
COMMAND_ERROR("Set fifo buffer size failed: %s", strerror(errno));
close(fifo_fd);
return -1;
}
@ -129,91 +115,93 @@ int open_fifo(const char *fifo_path)
return fifo_fd;
}
/* engine close log file */
void engine_close_log_file()
{
if (g_engine_log_fd != -1) {
close(g_engine_log_fd);
g_engine_log_fd = -1;
}
}
/* init log driver */
static int init_log_driver(const struct engine_log_config *log)
{
int i, driver;
for (i = LOG_PRIORITY_FATAL; i < LOG_PRIORITY_MAX; i++) {
if (!strcasecmp(g_engine_log_prio_name[i], log->priority)) {
for (i = ENGINE_LOG_FATAL; i < ENGINE_LOG_MAX; i++) {
if (strcasecmp(g_engine_log_prio_name[i], log->priority) == 0) {
g_engine_log_level = i;
break;
}
}
if (i == LOG_PRIORITY_MAX) {
fprintf(stderr, "Unable to parse logging level:%s\n", log->priority);
if (i == ENGINE_LOG_MAX) {
COMMAND_ERROR("Unable to parse logging level:%s", log->priority);
return -1;
}
driver = engine_change_str_logdriver_to_enum(log->driver);
if (driver < 0) {
fprintf(stderr, "Invalid log driver: %s\n", log->driver);
COMMAND_ERROR("Invalid log driver: %s", log->driver);
return -1;
}
g_engine_log_driver = driver;
return 0;
}
static inline bool check_log_config_args(const struct engine_log_config *log)
{
return (log == NULL || log->name == NULL || log->priority == NULL);
}
static int do_check_log_configs(const struct engine_log_config *log)
{
bool invalid_arg = false;
if (check_log_config_args(log)) {
COMMAND_ERROR("Invalid arguments");
return -1;
}
invalid_arg = ((log->file == NULL) && (g_engine_log_driver == LOG_DRIVER_FIFO));
if (invalid_arg) {
COMMAND_ERROR("Must set log file for driver %s", log->driver);
return -1;
}
return 0;
}
/* log enable */
int log_enable(const struct engine_log_config *log)
int engine_log_enable(const struct engine_log_config *log)
{
int nret = 0;
char *full_path = NULL;
if ((log->name == NULL) || (log->priority == NULL)) {
return -1;
}
if (g_engine_log_fd != -1) {
fprintf(stderr, "engine log already initialized\n");
COMMAND_ERROR("engine log already initialized");
return 0;
}
if (init_log_driver(log)) {
nret = do_check_log_configs(log);
if (nret != 0) {
return -1;
}
free(g_engine_log_vmname);
g_engine_log_vmname = util_strdup_s(log->name);
g_engine_log_quiet = log->quiet;
if ((log->file == NULL) || strcmp(log->file, "none") == 0) {
if (g_engine_log_driver == LOG_DRIVER_FIFO) {
fprintf(stderr, "Must set log file for driver %s\n", log->driver);
nret = -1;
}
goto out;
nret = init_log_driver(log);
if (nret != 0) {
return -1;
}
free(g_engine_log_module);
g_engine_log_module = util_strdup_s(log->name);
full_path = util_strdup_s(log->file);
if (full_path == NULL) {
fprintf(stderr, "Out of memory\n");
nret = -1;
nret = util_build_dir(full_path);
if (nret != 0) {
COMMAND_ERROR("failed to create dir for log file");
goto out;
}
if (util_build_dir(full_path)) {
fprintf(stderr, "failed to create dir for log file\n");
nret = -1;
goto out;
}
g_engine_log_fd = open_fifo(full_path);
if (g_engine_log_fd == -1) {
nret = -1;
}
out:
if (nret) {
if (nret != 0) {
if (g_engine_log_driver == LOG_DRIVER_FIFO) {
g_engine_log_driver = LOG_DRIVER_NOSET;
}
@ -223,59 +211,110 @@ out:
return nret;
}
/* engine log append */
int engine_log_append(const struct engine_log_event *event, const char *format, ...)
static int do_log_by_driver(const struct engine_log_object_metadata *metadata, const char *msg,
const char *date_time)
{
int rc;
va_list args;
char msg[MAX_MSG_LENGTH] = { 0 };
char date_time[ENGINE_LOG_TIME_SIZE] = { 0 };
struct timespec timestamp;
va_start(args, format);
rc = vsprintf(msg, format, args);
va_end(args);
if (rc < 0) {
rc = snprintf(msg, MAX_MSG_LENGTH, "%s", "!!LONG LONG A LOG!!");
if (rc < 0 || rc >= MAX_MSG_LENGTH) {
return 0;
}
}
if (clock_gettime(CLOCK_REALTIME, &timestamp) == -1) {
fprintf(stderr, "Failed to get real time\n");
return 0;
}
if (engine_unix_trans_to_utc(date_time, ENGINE_LOG_TIME_SIZE, &timestamp) < 0) {
return 0;
}
switch (g_engine_log_driver) {
case LOG_DRIVER_STDOUT:
if (g_engine_log_quiet) {
break;
}
log_append_stderr(event, date_time, msg);
do_stderr_log(metadata, date_time, msg);
break;
case LOG_DRIVER_FIFO:
if (g_engine_log_fd == -1) {
fprintf(stderr, "Do not set log file\n");
COMMAND_ERROR("Do not set log file\n");
return -1;
}
log_append_logfile(event, date_time, msg);
do_fifo_log(metadata, date_time, msg);
break;
case LOG_DRIVER_NOSET:
break;
default:
fprintf(stderr, "Invalid log driver\n");
COMMAND_ERROR("Invalid log driver\n");
return -1;
}
return 0;
}
static char *parse_timespec_to_human()
{
struct timespec timestamp;
struct tm ptm = {0};
char date_time[ENGINE_LOG_TIME_STR_MAX_LEN] = { 0 };
int nret;
#define SEC_TO_NSEC 1000000
#define FIRST_YEAR_OF_GMT 1900
if (clock_gettime(CLOCK_REALTIME, &timestamp) == -1) {
COMMAND_ERROR("Failed to get real time");
return 0;
}
if (localtime_r(&(timestamp.tv_sec), &ptm) == NULL) {
SYSERROR("Transfer timespec failed");
return NULL;
}
nret = snprintf(date_time, ENGINE_LOG_TIME_STR_MAX_LEN, "%04d%02d%02d%02d%02d%02d.%03ld",
ptm.tm_year + FIRST_YEAR_OF_GMT, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec,
timestamp.tv_nsec / SEC_TO_NSEC);
if (nret < 0 || nret >= ENGINE_LOG_TIME_STR_MAX_LEN) {
COMMAND_ERROR("Sprintf failed");
return NULL;
}
return util_strdup_s(date_time);
}
/* use to add log to driver */
int engine_log(const struct engine_log_object_metadata *metadata, const char *format, ...)
{
int rc;
char msg[MAX_MSG_LENGTH] = { 0 };
va_list args;
char *date_time = NULL;
int ret = 0;
va_start(args, format);
rc = vsnprintf(msg, MAX_MSG_LENGTH, format, args);
va_end(args);
if (rc < 0) {
rc = snprintf(msg, MAX_MSG_LENGTH, "%s", "!!LONG LONG A LOG!!");
if (rc < 0) {
return 0;
}
}
date_time = parse_timespec_to_human();
if (date_time == NULL) {
goto out;
}
ret = do_log_by_driver(metadata, msg, date_time);
out:
free(date_time);
return ret;
}
static void do_write_log_into_file(int log_fd, char *log_msg, size_t max_len, size_t write_size)
{
size_t size = 0;
size = write_size;
if (size > (max_len - 1)) {
size = max_len - 1;
}
log_msg[size] = '\n';
if (write_nointr(log_fd, log_msg, (size + 1)) == -1) {
COMMAND_ERROR("write log into logfile failed");
}
}
/* log append logfile */
void log_append_logfile(const struct engine_log_event *event, const char *timestamp, const char *msg)
static void do_fifo_log(const struct engine_log_object_metadata *metadata, const char *timestamp,
const char *msg)
{
char log_buffer[ENGINE_LOG_BUFFER_SIZE] = { 0 };
int log_fd = -1;
@ -283,7 +322,7 @@ void log_append_logfile(const struct engine_log_event *event, const char *timest
size_t size;
char *tmp_prefix = NULL;
if (event->priority > g_engine_log_level) {
if (metadata->level > g_engine_log_level) {
return;
}
log_fd = g_engine_log_fd;
@ -291,131 +330,47 @@ void log_append_logfile(const struct engine_log_event *event, const char *timest
return;
}
tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_vmname;
tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_module;
if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) {
tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH);
}
nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "",
timestamp, g_engine_log_prio_name[event->priority],
g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, event->locinfo->func,
event->locinfo->line, msg);
nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "",
timestamp, g_engine_log_prio_name[metadata->level], metadata->file,
metadata->func, metadata->line, msg);
if (nret < 0) {
nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s",
tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[event->priority],
g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file,
event->locinfo->func, event->locinfo->line, "Large log message");
if (nret < 0 || (size_t)nret >= sizeof(log_buffer)) {
nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s:%s:%d - %s",
tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[metadata->level],
metadata->file, metadata->func, metadata->line, "Large log message");
if (nret < 0) {
return;
}
}
size = (size_t)nret;
if (size > (sizeof(log_buffer) - 1)) {
size = sizeof(log_buffer) - 1;
}
log_buffer[size] = '\n';
if (write_nointr(log_fd, log_buffer, (size + 1)) == -1) {
fprintf(stderr, "write log into logfile failed");
}
do_write_log_into_file(log_fd, log_buffer, sizeof(log_buffer), size);
}
/* log append stderr */
void log_append_stderr(const struct engine_log_event *event, const char *timestamp, const char *msg)
static void do_stderr_log(const struct engine_log_object_metadata *metadata, const char *timestamp,
const char *msg)
{
char *tmp_prefix = NULL;
if (event->priority > g_engine_log_level) {
if (metadata->level > g_engine_log_level) {
return;
}
tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_vmname;
tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_module;
if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) {
tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH);
}
fprintf(stderr, "%15s %s %-8s ", tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[event->priority]);
fprintf(stderr, "%s - ", g_engine_log_vmname ? g_engine_log_vmname : "engine");
fprintf(stderr, "%s:%s:%d - ", event->locinfo->file, event->locinfo->func, event->locinfo->line);
fprintf(stderr, "%s", msg);
fprintf(stderr, "\n");
}
/* engine unix trans to utc */
int engine_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *time)
{
int64_t trans_to_days, all_days, age, doa, yoa, doy, nom, hours_to_sec, trans_to_sec;
int64_t real_year, real_day, real_month, real_hours, real_minutes, real_seconds;
char ns[LCR_NUMSTRLEN64] = { 0 };
int ret;
/* Transtate seconds to number of days. */
trans_to_days = time->tv_sec / 86400;
/* Calculate days from 0000-03-01 to 1970-01-01.Days base it */
all_days = trans_to_days + 719468;
/* compute the age.One age means 400 years(146097 days) */
age = (all_days >= 0 ? all_days : all_days - 146096) / 146097;
/* The day-of-age (doa) can then be found by subtracting the genumber */
doa = (all_days - age * 146097);
/* Calculate year-of-age (yoa, range [0, 399]) */
yoa = ((doa - doa / 1460) + (doa / 36524 - doa / 146096)) / 365;
/* Compute the year this moment */
real_year = yoa + age * 400;
/* Calculate the day-of-year */
doy = doa - (365 * yoa + yoa / 4 - yoa / 100);
/* Compute the month number. */
nom = (5 * doy + 2) / 153;
/* Compute the real_day. */
real_day = (doy - (153 * nom + 2) / 5) + 1;
/* Compute the correct month. */
real_month = nom + (nom < 10 ? 3 : -9);
/* Add one year before March */
if (real_month < 3) {
real_year++;
}
/* Translate days in the age to seconds. */
trans_to_sec = trans_to_days * 86400;
/* Compute the real_hours */
real_hours = (time->tv_sec - trans_to_sec) / 3600;
/* Translate the real hours to seconds. */
hours_to_sec = real_hours * 3600;
/* Calculate the real minutes */
real_minutes = ((time->tv_sec - trans_to_sec) - hours_to_sec) / 60;
/* Calculate the real seconds */
real_seconds = (((time->tv_sec - trans_to_sec) - hours_to_sec) - (real_minutes * 60));
ret = snprintf(ns, LCR_NUMSTRLEN64, "%ld", time->tv_nsec);
if (ret < 0 || ret >= LCR_NUMSTRLEN64) {
return -1;
}
/* Create the final timestamp */
ret = snprintf(buf, bufsize, "%" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 ".%.3s",
real_year, real_month, real_day, real_hours, real_minutes, real_seconds, ns);
if (ret < 0 || (size_t)ret >= bufsize) {
return -1;
}
return 0;
COMMAND_ERROR("%15s %s %-8s ", tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[metadata->level]);
COMMAND_ERROR("%s:%s:%d - ", metadata->file, metadata->func, metadata->line);
COMMAND_ERROR("%s\n", msg);
}
/* write nointr */
ssize_t write_nointr(int fd, const void *buf, size_t count)
static ssize_t write_nointr(int fd, const void *buf, size_t count)
{
ssize_t nret;
for (;;) {
@ -428,3 +383,4 @@ ssize_t write_nointr(int fd, const void *buf, size_t count)
}
return nret;
}

165
src/log.h
View File

@ -1,6 +1,6 @@
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
* lcr licensed under the Mulan PSL v1.
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* engine 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
@ -9,17 +9,14 @@
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: wujing
* Create: 2018-11-08
* Create: 2019-4-08
* Description: provide container log functions
******************************************************************************/
#ifndef __LCR_LOG_H
#define __LCR_LOG_H
#ifndef __ENGINE_LOG_H
#define __ENGINE_LOG_H
#include <syslog.h>
#include <stdbool.h>
#include <sys/time.h>
#include <errno.h>
#include <time.h>
#ifdef __cplusplus
extern "C" {
@ -29,151 +26,99 @@ extern "C" {
#define O_CLOEXEC 02000000
#endif
#ifndef F_DUPFD_CLOEXEC
#define F_DUPFD_CLOEXEC 1030
#endif
#define ENGINE_LOG_BUFFER_SIZE 4096
/* We're logging in seconds and nanoseconds. Assuming that the underlying
* datatype is currently at maximum a 64bit integer, we have a date string that
* is of maximum length (2^64 - 1) * 2 = (21 + 21) = 42.
* */
#define ENGINE_LOG_TIME_SIZE 42
#define ENGINE_LOG_TIME_STR_MAX_LEN 21
/* errmsg that defined in lcrc & lcrd */
#define DAEMON_ERROR_GRPC_INIT_STR "Init failed"
#define DAEMON_ERROR_GRPC_CONNENCT_STR "Can not connect with server.Is the docker dameon running on the host?"
#define DAEMON_ERROR_GRPC_SERVER_STR "Server internal error"
enum g_engine_log_driver {
enum engine_log_drivers {
LOG_DRIVER_STDOUT,
LOG_DRIVER_FIFO,
LOG_DRIVER_SYSLOG,
LOG_DRIVER_NOSET,
};
enum engine_log_level {
ENGINE_LOG_FATAL = 0,
ENGINE_LOG_ALERT,
ENGINE_LOG_CRIT,
ENGINE_LOG_ERROR,
ENGINE_LOG_WARN,
ENGINE_LOG_NOTICE,
ENGINE_LOG_INFO,
ENGINE_LOG_DEBUG,
ENGINE_LOG_TRACE,
ENGINE_LOG_MAX
};
struct engine_log_config {
const char *name;
const char *file;
const char *priority;
const char *prefix;
const char *driver;
bool quiet;
};
/* location information of the logging event */
struct engine_log_locinfo {
/* brief logging event object */
struct engine_log_object_metadata {
/* location information of the logging item */
const char *file;
const char *func;
int line;
int level;
};
#define ENGINE_LOG_LOCINFO_INIT \
{ \
.file = __FILE__, .func = __func__, .line = __LINE__, \
}
/* brief logging event object */
struct engine_log_event {
int priority;
struct engine_log_locinfo *locinfo;
};
extern void engine_close_log_file();
int log_enable(const struct engine_log_config *log);
int engine_log_enable(const struct engine_log_config *log);
void engine_set_log_prefix(const char *prefix);
void engine_free_log_prefix();
void engine_free_log_prefix(void);
int engine_change_str_logdriver_to_enum(const char *driver);
int engine_log(const struct engine_log_object_metadata *metadata, const char *format, ...);
int engine_log_append(const struct engine_log_event *event, const char *format, ...);
#define DEBUG(format, ...) \
do { \
struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \
struct engine_log_event event; \
event.locinfo = &locinfo; \
event.priority = LOG_DEBUG; \
(void)engine_log_append(&event, format, ##__VA_ARGS__); \
#define COMMON_LOG(loglevel, format, ...) \
do { \
struct engine_log_object_metadata meta = { \
.file = __FILENAME__, .func = __func__, .line = __LINE__, .level = loglevel, \
}; \
(void)engine_log(&meta, format, ##__VA_ARGS__); \
} while (0)
#define INFO(format, ...) \
do { \
struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \
struct engine_log_event event; \
event.locinfo = &locinfo; \
event.priority = LOG_INFO; \
(void)engine_log_append(&event, format, ##__VA_ARGS__); \
} while (0)
#define DEBUG(format, ...) \
COMMON_LOG(ENGINE_LOG_DEBUG, format, ##__VA_ARGS__)
#define NOTICE(format, ...) \
do { \
struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \
struct engine_log_event event; \
event.locinfo = &locinfo; \
event.priority = LOG_NOTICE; \
(void)engine_log_append(&event, format, ##__VA_ARGS__); \
} while (0)
#define INFO(format, ...) \
COMMON_LOG(ENGINE_LOG_INFO, format, ##__VA_ARGS__)
#define WARN(format, ...) \
do { \
struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \
struct engine_log_event event; \
event.locinfo = &locinfo; \
event.priority = LOG_WARNING; \
(void)engine_log_append(&event, format, ##__VA_ARGS__); \
} while (0)
#define NOTICE(format, ...) \
COMMON_LOG(ENGINE_LOG_NOTICE, format, ##__VA_ARGS__)
#define ERROR(format, ...) \
do { \
struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \
struct engine_log_event event; \
event.locinfo = &locinfo; \
event.priority = LOG_ERR; \
(void)engine_log_append(&event, format, ##__VA_ARGS__); \
} while (0)
#define WARN(format, ...) \
COMMON_LOG(ENGINE_LOG_WARN, format, ##__VA_ARGS__)
#define CRIT(format, ...) \
do { \
struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \
struct engine_log_event event; \
event.locinfo = &locinfo; \
event.priority = LOG_CRIT; \
(void)engine_log_append(&event, format, ##__VA_ARGS__); \
} while (0)
#define ERROR(format, ...) \
COMMON_LOG(ENGINE_LOG_ERROR, format, ##__VA_ARGS__)
#define ALERT(format, ...) \
do { \
struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \
struct engine_log_event event; \
event.locinfo = &locinfo; \
event.priority = LOG_ALERT; \
(void)engine_log_append(&event, format, ##__VA_ARGS__); \
} while (0)
#define CRIT(format, ...) \
COMMON_LOG(ENGINE_LOG_CRIT, format, ##__VA_ARGS__)
#define FATAL(format, ...) \
do { \
struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \
struct engine_log_event event; \
event.locinfo = &locinfo; \
event.priority = LOG_EMERG; \
(void)engine_log_append(&event, format, ##__VA_ARGS__); \
} while (0)
#define ALERT(format, ...) \
COMMON_LOG(ENGINE_LOG_ALERT, format, ##__VA_ARGS__)
#define FATAL(format, ...) \
COMMON_LOG(ENGINE_LOG_FATAL, format, ##__VA_ARGS__)
#define SYSERROR(format, ...) \
do { \
ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \
} while (0)
#define COMMAND_ERROR(fmt, args...) \
do { \
(void)fprintf(stderr, fmt "\n", ##args); \
#define COMMAND_ERROR(fmt, args...) \
do { \
(void)fprintf(stderr, fmt "\n", ##args); \
} while (0)
#ifdef __cplusplus
}
#endif
#endif /* __LCR_LOG_H */
#endif /* __ENGINE_LOG_H */