Merge branch 'master' of http://git.komect.net/ISG/secogateway
This commit is contained in:
commit
40bd7a61bc
|
@ -1,13 +1,17 @@
|
|||
#ifndef _ULOG_API_H
|
||||
#define _ULOG_API_H
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
#include "common_types.h"
|
||||
|
||||
#define MAX_MODULE_NAME_SZ 16
|
||||
|
||||
typedef struct _ulog {
|
||||
char module_name[MAX_MODULE_NAME_SZ];
|
||||
} ulog_t;
|
||||
|
||||
ulog_t *ulog_init(const char *module_name);
|
||||
ulog_t *ulog_init(const char *module_name, u8 is_print);
|
||||
void ulog_close(ulog_t *log);
|
||||
void ulog_record(const ulog_t *log, int level, const char *fmt, ...);
|
||||
|
||||
|
|
|
@ -251,15 +251,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*UserHandler.*</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
@ -14,21 +14,7 @@
|
|||
|
||||
package org.opendaylight.aaa.shiro.idm;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import org.opendaylight.aaa.api.ClaimCache;
|
||||
import org.opendaylight.aaa.AAAShiroProvider;
|
||||
import org.opendaylight.aaa.api.IDMStoreException;
|
||||
import org.opendaylight.aaa.api.IIDMStore;
|
||||
import org.opendaylight.aaa.api.model.IDMError;
|
||||
|
@ -37,7 +23,14 @@ import org.opendaylight.aaa.api.model.Users;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
@ -58,7 +51,7 @@ import java.util.Objects;
|
|||
public class UserHandler {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UserHandler.class);
|
||||
private static final String PW_PATTERN = "/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{8,}$/";
|
||||
private static final String PW_PATTERN = "^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{8,}$";
|
||||
/**
|
||||
* If a user is created through the <code>/auth/v1/users</code> rest
|
||||
* endpoint without a password, the default password is assigned to the
|
||||
|
@ -129,8 +122,7 @@ public class UserHandler {
|
|||
* Extracts the user represented by <code>id</code>. The password and salt
|
||||
* fields are redacted for security reasons.
|
||||
*
|
||||
* @param id
|
||||
* the unique id of representing the user account
|
||||
* @param id the unique id of representing the user account
|
||||
* @return A response with the user information, or internal error if one
|
||||
* occurs
|
||||
*/
|
||||
|
@ -170,10 +162,8 @@ public class UserHandler {
|
|||
* If a password is not provided, please ensure you change the default
|
||||
* password ASAP for security reasons!
|
||||
*
|
||||
* @param info
|
||||
* passed from Jersey
|
||||
* @param user
|
||||
* the user defined in the JSON payload
|
||||
* @param info passed from Jersey
|
||||
* @param user the user defined in the JSON payload
|
||||
* @return A response stating success or failure of user creation
|
||||
*/
|
||||
@POST
|
||||
|
@ -278,15 +268,13 @@ public class UserHandler {
|
|||
private Response providePasswordError(String s) {
|
||||
return new IDMError(407, s).response();
|
||||
}
|
||||
|
||||
/**
|
||||
* REST endpoint to update a user account.
|
||||
*
|
||||
* @param info
|
||||
* passed from Jersey
|
||||
* @param user
|
||||
* the user defined in the JSON payload
|
||||
* @param id
|
||||
* the unique id for the user that will be updated
|
||||
* @param info passed from Jersey
|
||||
* @param user the user defined in the JSON payload
|
||||
* @param id the unique id for the user that will be updated
|
||||
* @return A response stating success or failure of the user update
|
||||
*/
|
||||
@PUT
|
||||
|
@ -338,10 +326,8 @@ public class UserHandler {
|
|||
/**
|
||||
* REST endpoint to delete a user account.
|
||||
*
|
||||
* @param info
|
||||
* passed from Jersey
|
||||
* @param id
|
||||
* the unique id of the user which is being deleted
|
||||
* @param info passed from Jersey
|
||||
* @param id the unique id of the user which is being deleted
|
||||
* @return A response stating success or failure of user deletion
|
||||
*/
|
||||
@DELETE
|
||||
|
@ -368,10 +354,8 @@ public class UserHandler {
|
|||
/**
|
||||
* Creates a <code>Response</code> related to an internal server error.
|
||||
*
|
||||
* @param verbal
|
||||
* such as "creating", "deleting", "updating"
|
||||
* @param ex
|
||||
* The exception, which is logged locally
|
||||
* @param verbal such as "creating", "deleting", "updating"
|
||||
* @param ex The exception, which is logged locally
|
||||
* @return A response containing internal error with specific reasoning
|
||||
*/
|
||||
private Response internalError(final String verbal, final Exception ex) {
|
||||
|
@ -384,8 +368,7 @@ public class UserHandler {
|
|||
* Creates a <code>Response</code> related to the user not providing a
|
||||
* required field.
|
||||
*
|
||||
* @param fieldName
|
||||
* the name of the field which is missing
|
||||
* @param fieldName the name of the field which is missing
|
||||
* @return A response explaining that the request is missing a field
|
||||
*/
|
||||
private Response missingRequiredField(final String fieldName) {
|
||||
|
@ -400,10 +383,8 @@ public class UserHandler {
|
|||
* Creates a <code>Response</code> related to the user providing a field
|
||||
* that is too long.
|
||||
*
|
||||
* @param fieldName
|
||||
* the name of the field that is too long
|
||||
* @param maxFieldLength
|
||||
* the maximum length of <code>fieldName</code>
|
||||
* @param fieldName the name of the field that is too long
|
||||
* @param maxFieldLength the maximum length of <code>fieldName</code>
|
||||
* @return A response containing the bad field and the maximum field length
|
||||
*/
|
||||
private Response providedFieldTooLong(final String fieldName, final int maxFieldLength) {
|
||||
|
@ -414,10 +395,8 @@ public class UserHandler {
|
|||
* Creates the client-facing message related to the user providing a field
|
||||
* that is too long.
|
||||
*
|
||||
* @param fieldName
|
||||
* the name of the field that is too long
|
||||
* @param maxFieldLength
|
||||
* the maximum length of <code>fieldName</code>
|
||||
* @param fieldName the name of the field that is too long
|
||||
* @param maxFieldLength the maximum length of <code>fieldName</code>
|
||||
* @return a response containing the too long field and its length
|
||||
*/
|
||||
private static String getProvidedFieldTooLongMessage(final String fieldName, final int maxFieldLength) {
|
||||
|
@ -430,8 +409,7 @@ public class UserHandler {
|
|||
* Prepares a user account for output by redacting the appropriate fields.
|
||||
* This method side-effects the <code>user</code> parameter.
|
||||
*
|
||||
* @param user
|
||||
* the user account which will have fields redacted
|
||||
* @param user the user account which will have fields redacted
|
||||
*/
|
||||
private static void redactUserPasswordInfo(final User user) {
|
||||
user.setPassword(REDACTED_PASSWORD);
|
||||
|
@ -441,8 +419,7 @@ public class UserHandler {
|
|||
/**
|
||||
* Validate the input field length.
|
||||
*
|
||||
* @param inputField
|
||||
* the field to check
|
||||
* @param inputField the field to check
|
||||
* @return true if input field bigger than the MAX_FIELD_LEN
|
||||
*/
|
||||
private boolean checkInputFieldLength(final String inputField) {
|
||||
|
|
|
@ -14,24 +14,24 @@
|
|||
|
||||
package org.opendaylight.aaa.shiro.idm.rest.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.ws.rs.NotFoundException;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Response;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.opendaylight.aaa.api.model.IDMError;
|
||||
import org.opendaylight.aaa.api.model.User;
|
||||
import org.opendaylight.aaa.api.model.Users;
|
||||
|
||||
@Ignore
|
||||
import javax.ws.rs.NotFoundException;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class UserHandlerTest extends HandlerTest {
|
||||
|
||||
@Test
|
||||
|
@ -61,12 +61,14 @@ public class UserHandlerTest extends HandlerTest {
|
|||
// check create user
|
||||
Map<String, String> usrData = new HashMap<>();
|
||||
usrData.put("name", "usr1");
|
||||
usrData.put("description", "test user");
|
||||
usrData.put("enabled", "true");
|
||||
// usrData.put("description", "test user");
|
||||
// usrData.put("enabled", "true");
|
||||
usrData.put("email", "user1@usr.org");
|
||||
usrData.put("password", "ChangeZbadPa$$w0rd");
|
||||
usrData.put("domainid", "0");
|
||||
Response clientResponse = target("/v1/users").request().post(entity(usrData));
|
||||
Response clientResponse = target("/v1/users")
|
||||
.request()
|
||||
.post(entity(usrData));
|
||||
assertEquals(201, clientResponse.getStatus());
|
||||
|
||||
// check create user missing name data
|
||||
|
@ -101,8 +103,8 @@ public class UserHandlerTest extends HandlerTest {
|
|||
// Bug 8382: if a user id is specified, 400 is returned
|
||||
usrData = new HashMap<>();
|
||||
usrData.put("name", "usr1");
|
||||
usrData.put("description", "test user");
|
||||
usrData.put("enabled", "true");
|
||||
// usrData.put("description", "test user");
|
||||
// usrData.put("enabled", "true");
|
||||
usrData.put("email", "user1@usr.org");
|
||||
usrData.put("password", "ChangeZbadPa$$w0rd");
|
||||
usrData.put("userid", "userid");
|
||||
|
@ -113,10 +115,10 @@ public class UserHandlerTest extends HandlerTest {
|
|||
}
|
||||
/**
|
||||
* Revision history
|
||||
*
|
||||
* <p>
|
||||
* -------------------------------------------------------------------------
|
||||
* Date Author Note
|
||||
*
|
||||
* <p>
|
||||
* -------------------------------------------------------------------------
|
||||
* 2019/7/3 Dong Xiancun creat
|
||||
*/
|
||||
|
|
|
@ -139,11 +139,6 @@
|
|||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*UserHandler.*</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
6
Makefile
6
Makefile
|
@ -162,14 +162,14 @@ endif
|
|||
|
||||
ulog:
|
||||
ifeq ($(OPT), clean)
|
||||
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.syslog_sched.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=syslog-sched
|
||||
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.api.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=ulog-api
|
||||
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.log_sched.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=log-sched
|
||||
else ifeq ($(OPT), install)
|
||||
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.syslog_sched.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=syslog-sched
|
||||
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.api.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=ulog-api
|
||||
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.log_sched.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=log-sched
|
||||
else
|
||||
$(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.ulog.syslog_sched.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=syslog-sched
|
||||
$(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.ulog.api.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=ulog-api
|
||||
$(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.ulog.log_sched.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=log-sched
|
||||
endif
|
||||
|
||||
database:
|
||||
|
|
|
@ -34,7 +34,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS)
|
|||
PLAT_ARM64_SRCS = $(COMMON_SRCS)
|
||||
|
||||
# gcc CFLAGS
|
||||
PLAT_ARM64_CFLAGS := -fPIC -I../../Common
|
||||
PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../user/ulog
|
||||
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
|
||||
|
||||
|
||||
|
@ -44,6 +44,7 @@ PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
|
|||
|
||||
#gcc libs
|
||||
|
||||
|
||||
# this line must be at below of thus, because of...
|
||||
include ../../Common/common.Makefile
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# target name, the target name must have the same name of c source file
|
||||
TARGET_NAME=syslog-sched
|
||||
TARGET_NAME=log-sched
|
||||
|
||||
# target
|
||||
# for linux module driver: KO
|
||||
|
@ -21,20 +21,20 @@ DEBUG = TRUE
|
|||
PLAT_LINUX ?= TRUE
|
||||
PLAT_ARM64 ?= TRUE
|
||||
|
||||
VPATH = ../user/ulog/syslog-schedule
|
||||
VPATH = ../user/ulog/log-sched
|
||||
|
||||
# source code
|
||||
|
||||
# set the source file, don't used .o because of ...
|
||||
|
||||
COMMON_SRCS = log_file.c log_sched.c
|
||||
COMMON_SRCS = log_file.c log_console.c log_common.c log_sched.c
|
||||
|
||||
# MRS Board Source Files
|
||||
PLAT_LINUX_SRCS = $(COMMON_SRCS)
|
||||
PLAT_ARM64_SRCS = $(COMMON_SRCS)
|
||||
|
||||
# gcc CFLAGS
|
||||
PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/rpc
|
||||
PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/rpc -I../user/ulog
|
||||
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
|
||||
|
||||
|
||||
|
@ -42,17 +42,17 @@ PLAT_ARM64_LDFLAGS := -lpthread
|
|||
PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
|
||||
|
||||
#gcc libs
|
||||
ARM64_LIBS := ./libopenrpc-arm64.so ../thirdparty/arm64/libev-arm64.so
|
||||
LINUX_LIBS := ./libopenrpc-linux.so ../thirdparty/x86_64/libev-linux.so
|
||||
ARM64_LIBS := ./libopenrpc-arm64.so ./libulogapi-arm64.so ../thirdparty/arm64/libev-arm64.so
|
||||
LINUX_LIBS := ./libopenrpc-linux.so ./libulogapi-linux.so ../thirdparty/x86_64/libev-linux.so
|
||||
|
||||
ifeq ($(PLAT_ARM64), TRUE)
|
||||
DEPEND_LIB += ./debug/libopenrpc-arm64.so
|
||||
USER_CLEAN_ITEMS += ./libopenrpc-arm64.so
|
||||
DEPEND_LIB += ./debug/libopenrpc-arm64.so ./debug/libulogapi-arm64.so
|
||||
USER_CLEAN_ITEMS += ./libopenrpc-arm64.so ./libulogapi-arm64.so
|
||||
endif
|
||||
|
||||
ifeq ($(PLAT_LINUX), TRUE)
|
||||
DEPEND_LIB += ./debug/libopenrpc-linux.so
|
||||
USER_CLEAN_ITEMS += ./libopenrpc-linux.so
|
||||
DEPEND_LIB += ./debug/libopenrpc-linux.so ./debug/libulogapi-linux.so
|
||||
USER_CLEAN_ITEMS += ./libopenrpc-linux.so ./libulogapi-linux.so
|
||||
endif
|
||||
|
||||
# this line must be at below of thus, because of...
|
|
@ -7,6 +7,8 @@
|
|||
#define MODULE_NAME_LEN 32
|
||||
#define IP_STR_LEN 32
|
||||
|
||||
#define RPC_MODULE_SYSLOG_NAME "syslog-schedule-rpc"
|
||||
|
||||
struct _rpc_module {
|
||||
char module_name[MODULE_NAME_LEN];
|
||||
char host[IP_STR_LEN];
|
||||
|
@ -18,7 +20,8 @@ typedef struct _rpc_module rpc_module;
|
|||
|
||||
#define MODULE_REG_ARRAY \
|
||||
{ \
|
||||
{"ConfigManger#0", "127.0.0.1", 10002, 1} \
|
||||
{"ConfigManger#0", "127.0.0.1", 10002, 1}, \
|
||||
{RPC_MODULE_SYSLOG_NAME, "127.0.0.1", 10003, 2} \
|
||||
}
|
||||
|
||||
#endif /* RPC_MODULE_H_ */
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "ulog.h"
|
||||
#include "log_common.h"
|
||||
|
||||
#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n"
|
||||
|
||||
#define BAK_FILE "/tmp/%s"
|
||||
|
||||
int write_log_conf(const u8 level, const char *conf_path, const char *conf_file,
|
||||
const char *rediect_path, const char *filter_mod)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
int ret = 1;
|
||||
u8 exist_backup = 1;
|
||||
|
||||
/********** rsyslog configure **********/
|
||||
char conf_path_file[MAX_LINE_SIZE];
|
||||
snprintf(conf_path_file, sizeof(conf_path_file), "%s%s", conf_path, conf_file);
|
||||
|
||||
char bak_file[MAX_LINE_SIZE];
|
||||
snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file);
|
||||
if (rename(conf_path_file, bak_file) < 0) {
|
||||
if (errno == ENOENT) {
|
||||
exist_backup = 0;
|
||||
ULOG_INFO(g_log, "Been not exist, Configure file:%s is need to backup");
|
||||
} else {
|
||||
ULOG_ERR(g_log, "Baking configure file:%s is failure:%s %d", conf_path_file, strerror(errno), errno);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
fp = fopen(conf_path_file, "w");
|
||||
if (fp == NULL) {
|
||||
ULOG_ERR(g_log, "Opening log configure file:%s is failure:%s", conf_path_file, strerror(errno));
|
||||
goto END;
|
||||
}
|
||||
|
||||
int i;
|
||||
char line[MAX_LINE_SIZE + 100] = {0};
|
||||
if ((filter_mod != NULL) && (strlen(filter_mod) > 0)) {
|
||||
snprintf(line, sizeof(line), FILTER_CONTENT, filter_mod);
|
||||
if (fputs(line, fp) == EOF) {
|
||||
ULOG_ERR(g_log, "Message filter:%s of configure file which is written is failure:%s",
|
||||
filter_mod, strerror(errno));
|
||||
goto END;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char tmp[20];
|
||||
line[0] = '\0'; // 清零
|
||||
for (i = 0; i <= level;) {
|
||||
if (snprintf(tmp, sizeof(tmp), "*.=%s", g_level_array[i].str) < 0) {
|
||||
ULOG_ERR(g_log, "Setting content of log file configure is failure");
|
||||
goto END;
|
||||
}
|
||||
strcat(line, tmp);
|
||||
i++;
|
||||
if (level >= i) {
|
||||
strcat(line, ";");
|
||||
}
|
||||
}
|
||||
strcat(line, " ");
|
||||
|
||||
strcat(line, rediect_path);
|
||||
|
||||
if (fputs(line, fp) == EOF) {
|
||||
ULOG_ERR(g_log, "Configure file which is written is failure:%s", strerror(errno));
|
||||
goto END;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
END:
|
||||
if (fp != NULL) {
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
if ((ret == 1) && (exist_backup == 1)) {
|
||||
// 回复备份配置
|
||||
if (rename(bak_file, conf_path_file) < 0) {
|
||||
ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int modify_authorizing(const char *redirect_path)
|
||||
{
|
||||
if (chmod(redirect_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) {
|
||||
ULOG_ERR(g_log, "Authorizing of %s which is modified is failure:%s", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef _LOG_COMMON_H
|
||||
#define _LOG_COMMON_H
|
||||
|
||||
#include "ulog_api.h"
|
||||
|
||||
#define MAX_LINE_SIZE 1024
|
||||
|
||||
#define LOG_CONF_PATH "/etc/rsyslog.d/"
|
||||
|
||||
typedef struct _level_str {
|
||||
u32 level;
|
||||
char str[10];
|
||||
} level_str_t;
|
||||
|
||||
extern ulog_t *g_log;
|
||||
|
||||
static level_str_t g_level_array[] = {
|
||||
{LOG_EMERG, "emerg"},
|
||||
{LOG_ALERT, "alert"},
|
||||
{LOG_CRIT, "crit"},
|
||||
{LOG_ERR, "err"},
|
||||
{LOG_WARNING, "warn"},
|
||||
{LOG_NOTICE, "notice"},
|
||||
{LOG_INFO, "info"},
|
||||
{LOG_DEBUG, "debug"}
|
||||
};
|
||||
|
||||
int write_log_conf(const u8 level, const char *conf_path, const char *conf_file,
|
||||
const char *rediect_path, const char *filter_mod);
|
||||
int modify_authorizing(const char *redirect_path);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,35 @@
|
|||
#include "log_console.h"
|
||||
#include "log_common.h"
|
||||
|
||||
#define LOG_CONF_COSOLE_FILE_NAME "log-console.conf"
|
||||
|
||||
#define LOG_REDIRECT_CONSOLE "/dev/console"
|
||||
|
||||
static int config_log_console(const log_console_t *conf)
|
||||
{
|
||||
if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, LOG_REDIRECT_CONSOLE, conf->module_name) != 0) {
|
||||
ULOG_ERR(g_log, "configure of log conosle which is written is failure");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (modify_authorizing(LOG_REDIRECT_CONSOLE) != 0) {
|
||||
ULOG_ERR(g_log, "Modifying authorizing of %s is failure", LOG_REDIRECT_CONSOLE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer data)
|
||||
{
|
||||
u32 need_len = sizeof(log_console_t);
|
||||
if (input_len < need_len) {
|
||||
ULOG_WARNING(g_log,
|
||||
"The input paramter of rpc log console is needed length of %u, but the actual length is %u",
|
||||
need_len, input_len);
|
||||
return;
|
||||
}
|
||||
|
||||
config_log_console((const log_console_t *)input);
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef _LOG_CONSOLE_H
|
||||
#define _LOG_CONSOLE_H
|
||||
|
||||
#include "ulog_api.h"
|
||||
#include "common_types.h"
|
||||
#include "rpc_common.h"
|
||||
|
||||
typedef struct _log_console {
|
||||
u8 level;
|
||||
u8 on;
|
||||
char module_name[MAX_MODULE_NAME_SZ];
|
||||
} log_console_t;
|
||||
|
||||
void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer data);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,137 @@
|
|||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "log_file.h"
|
||||
#include "log_common.h"
|
||||
|
||||
#define LOG_CONF_FILE_NAME "log-file.conf"
|
||||
|
||||
#define LOG_LOGRATATE_FILE_NAME "/etc/logrotate.d/log-syslog"
|
||||
|
||||
#define DEFAULT_LOG_FILE "/var/log/syslog-test"
|
||||
#define DEFAULT_LOG_DEL_OVER_DAYS (30 * 6)
|
||||
|
||||
#define LOGROTATE_CONF "%s\n" \
|
||||
"{\n" \
|
||||
" rotate %u\n" \
|
||||
" daily\n" \
|
||||
" missingok\n" \
|
||||
"%s" \
|
||||
" postrotate\n" \
|
||||
" /usr/lib/rsyslog/rsyslog-rotate\n" \
|
||||
" endscript\n" \
|
||||
"}"
|
||||
|
||||
#define STR_COMPRESS " compress\n"
|
||||
|
||||
/*
|
||||
typedef struct _level_str {
|
||||
u32 level;
|
||||
char str[10];
|
||||
} level_str_t;
|
||||
|
||||
|
||||
static level_str_t g_level_array[] = {
|
||||
{LOG_EMERG, "emerg"},
|
||||
{LOG_ALERT, "alert"},
|
||||
{LOG_CRIT, "crit"},
|
||||
{LOG_ERR, "err"},
|
||||
{LOG_WARNING, "warn"},
|
||||
{LOG_NOTICE, "notice"},
|
||||
{LOG_INFO, "info"},
|
||||
{LOG_DEBUG, "debug"}
|
||||
};
|
||||
*/
|
||||
|
||||
#define MAX_LOG_LEVEL_VALUE (sizeof(g_level_array) / sizeof(level_str_t))
|
||||
|
||||
static int write_logratate_conf(const log_file_t *conf, const char *log_path)
|
||||
{
|
||||
int ret = 1;
|
||||
FILE *fp = NULL;
|
||||
/********** logrotate **********/
|
||||
char str_compress[sizeof(STR_COMPRESS)] = {0};
|
||||
if (conf->is_compress == LOG_COMPRESS) {
|
||||
strncpy(str_compress, STR_COMPRESS, sizeof(str_compress));
|
||||
}
|
||||
|
||||
u32 days = DEFAULT_LOG_DEL_OVER_DAYS;
|
||||
if (conf->del_over_days > 0) {
|
||||
days = conf->del_over_days;
|
||||
}
|
||||
|
||||
char line[1024] = {0};
|
||||
if (snprintf(line, sizeof(line), LOGROTATE_CONF, log_path, days, str_compress) < 0) {
|
||||
ULOG_ERR(g_log, "Setting content of logratote is failure");
|
||||
goto END;
|
||||
}
|
||||
|
||||
|
||||
fp = fopen(LOG_LOGRATATE_FILE_NAME, "w");
|
||||
if (fp == NULL) {
|
||||
ULOG_ERR(g_log, "Opening logratate file:%s is failure:%s", LOG_CONF_FILE_NAME, strerror(errno));
|
||||
goto END;
|
||||
}
|
||||
|
||||
if (fputs(line, fp) == EOF) {
|
||||
ULOG_ERR(g_log, "Configure file of logratate which is written is failure:%s", strerror(errno));
|
||||
goto END;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
END:
|
||||
if (fp != NULL) {
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int conf_log_file(const log_file_t *conf)
|
||||
{
|
||||
int ret = 1;
|
||||
u32 max_level = MAX_LOG_LEVEL_VALUE;
|
||||
if (conf->level > max_level) {
|
||||
ULOG_WARNING(g_log, "Configure log level:%u more than max value:%u", conf->level, max_level);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char path[sizeof(conf->path)];
|
||||
if (strlen(conf->path) > 0) {
|
||||
strncpy(path, conf->path, sizeof(path));
|
||||
} else {
|
||||
strncpy(path, DEFAULT_LOG_FILE, sizeof(path));
|
||||
}
|
||||
|
||||
if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, path, NULL) != 0) {
|
||||
ULOG_ERR(g_log, "Log configure which is written is failure");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/********** logrotate **********/
|
||||
if (write_logratate_conf(conf, path) != 0) {
|
||||
ULOG_ERR(g_log, "Logratate configure which is written is failure");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
if (conf->del_over_size > 0) {
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data)
|
||||
{
|
||||
u32 need_len = sizeof(log_file_t);
|
||||
if (input_len < need_len) {
|
||||
ULOG_WARNING(g_log,
|
||||
"The input paramter of rpc log file is needed length of %u, but the actual length is %u",
|
||||
need_len, input_len);
|
||||
return;
|
||||
}
|
||||
|
||||
conf_log_file((const log_file_t *)input);
|
||||
//rpc_return_null(conn);
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef __LOG_H
|
||||
#define __LOG_H
|
||||
|
||||
#include <syslog.h>
|
||||
#include <linux/limits.h>
|
||||
|
||||
#include "common_types.h"
|
||||
#include "rpc_common.h"
|
||||
#include "log_common.h"
|
||||
|
||||
#define MAX_LOG_PATH MAX_LINE_SIZE
|
||||
|
||||
typedef enum {
|
||||
LOG_UNCOMPRESS = 0,
|
||||
LOG_COMPRESS
|
||||
} log_compress_t;
|
||||
|
||||
typedef struct _log_file {
|
||||
u8 level;
|
||||
char path[MAX_LOG_PATH];
|
||||
log_compress_t is_compress;
|
||||
u32 del_over_days;
|
||||
u64 del_over_size;
|
||||
} log_file_t;
|
||||
|
||||
void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data);
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,72 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "rpc_server.h"
|
||||
#include "ulog_api.h"
|
||||
#include "log_file.h"
|
||||
#include "log_console.h"
|
||||
#include "rpc_module.h"
|
||||
|
||||
|
||||
#define LOG_SCHED_MODULE_NAME "log-sched"
|
||||
#define SERVICE_LOG_FILE_NAME "log-file"
|
||||
|
||||
ulog_t *g_log = NULL;
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int run_daemon = 0;
|
||||
char *options = "d";
|
||||
int opt;
|
||||
|
||||
while ((opt = getopt(argc, argv, options)) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
run_daemon = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
g_log = ulog_init(LOG_SCHED_MODULE_NAME, !run_daemon);
|
||||
if (g_log == NULL) {
|
||||
fprintf(stderr, "Initiating ulog is failure");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (run_daemon) {
|
||||
if (daemon(0, 0) == -1) {
|
||||
ULOG_ERR(g_log, "Setting daemon running is failure:%s", strerror(errno));
|
||||
goto END;
|
||||
}
|
||||
}
|
||||
|
||||
rpc_server *server = rpc_server_create_ex(RPC_MODULE_SYSLOG_NAME);
|
||||
if (server == NULL)
|
||||
{
|
||||
ULOG_ERR(g_log, "start server error");
|
||||
return 1;
|
||||
|
||||
}
|
||||
ULOG_INFO(g_log, "Server of log schedule is started");
|
||||
|
||||
/* 注册配置处理函数 */
|
||||
rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", rpc_conf_log_file);
|
||||
|
||||
log_file_t log = {0};
|
||||
log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS;
|
||||
log.del_over_days = 10;
|
||||
log.level = LOG_DEBUG;
|
||||
rpc_conf_log_file(NULL, &log, sizeof(log), NULL);
|
||||
|
||||
log_console_t console = {0};
|
||||
strcpy(console.module_name, "111");
|
||||
console.level = LOG_DEBUG;
|
||||
console.on = 1;
|
||||
rpc_conf_log_console(NULL, &console, sizeof(console), NULL);
|
||||
END:
|
||||
ulog_close(g_log);
|
||||
return 0;
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
#include "log_file.h"
|
||||
|
||||
int conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
#ifndef __LOG_H
|
||||
#define __LOG_H
|
||||
|
||||
#include <linux/limits.h>
|
||||
#include "common_types.h"
|
||||
#include "rpc_common.h"
|
||||
|
||||
|
||||
typedef struct _log_file {
|
||||
u8 level;
|
||||
char path[PATH_MAX];
|
||||
u32 compress_over_days;
|
||||
u32 del_over_days;
|
||||
u64 compress_over_size;
|
||||
u64 del_over_size;
|
||||
} log_file_t;
|
||||
|
||||
int conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data);
|
||||
|
||||
#endif
|
|
@ -1,41 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "log_file.h"
|
||||
|
||||
#define SYSLOG_SCHED_RPC_NAME "syslog_schedule_rpc"
|
||||
|
||||
#define SERVICE_LOG_FILE_NAME "log_file"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int run_daemon = 0;
|
||||
char *options = "d";
|
||||
int opt;
|
||||
|
||||
while ((opt = getopt(argc, argv, options)) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
run_daemon = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (run_daemon) {
|
||||
if (daemon(0, 0) == -1) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
rpc_server *server = rpc_server_create_ex(SYSLOG_SCHED_RPC_NAME);
|
||||
if (server = NULL)
|
||||
{
|
||||
printf("start server error\n");
|
||||
return 1;
|
||||
|
||||
}
|
||||
printf("Server of log schedule is started\n");
|
||||
|
||||
/* 注册配置处理函数 */
|
||||
rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", conf_log_file);
|
||||
}
|
|
@ -1,14 +1,16 @@
|
|||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "ulog.h"
|
||||
#include "common_types.h"
|
||||
#include "ulog_api.h"
|
||||
|
||||
#define LOG_MSG_SZ 1024
|
||||
|
||||
ulog_t *ulog_init(const char *module_name)
|
||||
ulog_t *ulog_init(const char *module_name, u8 is_print)
|
||||
{
|
||||
ulog_t *log;
|
||||
u32 len = sizeof(module_name);
|
||||
u32 len = strlen(module_name);
|
||||
|
||||
if (len > MAX_MODULE_NAME_SZ) {
|
||||
fprintf(stderr, "The length:%d of module_name can't more than %d", len, MAX_MODULE_NAME_SZ);
|
||||
|
@ -22,30 +24,35 @@ ulog_t *ulog_init(const char *module_name)
|
|||
}
|
||||
strncpy(log->module_name, module_name, len);
|
||||
|
||||
int opt = LOG_PERROR |LOG_PID;
|
||||
if (is_print > 0) {
|
||||
opt |= LOG_CONS;
|
||||
}
|
||||
openlog(log->module_name, opt, LOG_USER);
|
||||
|
||||
return log;
|
||||
}
|
||||
|
||||
void ulog_close(ulog_t *log)
|
||||
{
|
||||
if (log == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (log != NULL) {
|
||||
free(log);
|
||||
}
|
||||
closelog();
|
||||
}
|
||||
|
||||
void ulog_record(const ulog_t *log, int level, const char *fmt, ...)
|
||||
{
|
||||
if (log == NULL) {
|
||||
perror("Log is null");
|
||||
fprintf(stderr, "Log is null");
|
||||
return;
|
||||
}
|
||||
|
||||
char log_buf[LOG_MSG_SZ];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vsnprintf(log_buf, sizeof(log_buf), log_buf, args);
|
||||
vsnprintf(log_buf, sizeof(log_buf), fmt, args);
|
||||
va_end(args);
|
||||
syslog(level, "[%s] %s", log->module_name, log_buf);
|
||||
syslog(level, MODULE_FMT" %s", log->module_name, log_buf);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef _ULOG_H
|
||||
#define _ULOG_H
|
||||
|
||||
#define MODULE_FMT "[%s]"
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue