log管理器内存分配池化,修复部分log写入部分与内存池部分存在的恶性bug
This commit is contained in:
@ -2,9 +2,10 @@
|
||||
#define SEVERCONFG
|
||||
|
||||
/*------日志管理---------*/
|
||||
#define MAX_LOG 64
|
||||
#define MAX_LOG 50
|
||||
#define MAX_LOG_LENGTH 4080
|
||||
#define INFO_LENGTH 8
|
||||
#define LOG_SLEEP_LENGTH 1
|
||||
/*------日志管理---------*/
|
||||
|
||||
/*-------终端管理-------*/
|
||||
@ -25,8 +26,8 @@
|
||||
/*------解释器管理-------*/
|
||||
|
||||
/*------内存池管理------*/
|
||||
#define MAX_MEM_SIZE 256
|
||||
#define COMINE_MEM_SIZE 192
|
||||
#define MAX_MEM_SIZE 512
|
||||
#define COMINE_MEM_SIZE 448
|
||||
#define MEM_BLOCK_SIZE 4096
|
||||
#define POOL_EXPEND_ID 3
|
||||
#define POOL_EXPEND_SIZE 4
|
||||
|
||||
@ -9,24 +9,22 @@
|
||||
#include "interpreter.h"
|
||||
#include "tools/pkgmanager/pkginstall.h"
|
||||
|
||||
int inter_in_log(const char *log,log_manager *manager)
|
||||
int inter_in_log(const char *log,const char *info,log_manager *manager)
|
||||
{
|
||||
if(strlen(log)>1024)
|
||||
return -1;
|
||||
logs* logss = (logs*)malloc(sizeof(logs));
|
||||
memcpy(logss->log,log,strlen(log));
|
||||
manager->in_log(logss,manager);
|
||||
manager->in_log(manager,log,info);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager)
|
||||
{
|
||||
printf("SYS:prepare env\n");
|
||||
inter_in_log("SYS:prepare env\n",log_manager);
|
||||
inter_in_log("prepare env\n","SYS",log_manager);
|
||||
printf("SYS:env ready\n");
|
||||
inter_in_log("SYS:env ready\n",log_manager);
|
||||
inter_in_log("env ready\n","SYS",log_manager);
|
||||
printf("SYS:loading cmd_dic\n");
|
||||
inter_in_log("SYS:loading cmd_dic\n",log_manager);
|
||||
inter_in_log("loading cmd_dic\n","SYS",log_manager);
|
||||
sprintf(cmd_dic[0].name, "pkginstall");
|
||||
cmd_dic[0].cmd = INSTALL;
|
||||
|
||||
@ -37,7 +35,7 @@ int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager
|
||||
cmd_dic[2].cmd = QUIT;
|
||||
|
||||
printf("SYS:cmd_dir load complite\n");
|
||||
inter_in_log("SYS:cmd_dir load complite\n",log_manager);
|
||||
inter_in_log("cmd_dir load complite\n","SYS",log_manager);
|
||||
|
||||
for(int i =0;i<10;i++)
|
||||
{
|
||||
@ -45,7 +43,7 @@ int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager
|
||||
}
|
||||
self->arg = NULL;
|
||||
printf("SYS:Creating ctl fifo\n");
|
||||
inter_in_log("SYS:Creating ctl fifo\n",log_manager);
|
||||
inter_in_log("Creating ctl fifo\n","SYS",log_manager);
|
||||
memcpy(self->fifofd,fifo,2*sizeof(int));
|
||||
self->log_manager = log_manager;
|
||||
}
|
||||
@ -71,7 +69,7 @@ int get_args(ctx *self)
|
||||
arg->next = (args*)malloc(sizeof(args));
|
||||
if(arg->next == NULL){
|
||||
perror("ERROR:fail to get mem");
|
||||
inter_in_log("ERROR:fail to get mem\n",self->log_manager);
|
||||
inter_in_log("fail to get mem\n","ERROR",self->log_manager);
|
||||
return -1;
|
||||
}
|
||||
arg = arg->next;
|
||||
@ -146,31 +144,31 @@ int exec(const int command,ctx *all_ctx)
|
||||
{
|
||||
case BAD_INPUT:
|
||||
printf("SYS:bad input,try again\n");
|
||||
inter_in_log("SYS:bad input,try again\n",all_ctx->log_manager);
|
||||
inter_in_log("bad input,try again\n","SYS",all_ctx->log_manager);
|
||||
return BAD_INPUT;
|
||||
|
||||
case INSTALL:
|
||||
if(all_ctx->arg == NULL){
|
||||
printf("SYS:Missing args\n");
|
||||
inter_in_log("SYS:Missng args\n",all_ctx->log_manager);
|
||||
inter_in_log("Missng args\n","SYS",all_ctx->log_manager);
|
||||
return 1;
|
||||
}
|
||||
printf("SYS:init pkgmanager\n");
|
||||
inter_in_log("SYS:init pkgmanager\n",all_ctx->log_manager);
|
||||
inter_in_log("init pkgmanager\n","SYS",all_ctx->log_manager);
|
||||
pkger *manager = init_pkginstaller();
|
||||
printf("SYS:installing\n");
|
||||
inter_in_log("SYS:installing\n",all_ctx->log_manager);
|
||||
inter_in_log("installing\n","SYS",all_ctx->log_manager);
|
||||
manager->packup(manager);
|
||||
return 1;
|
||||
|
||||
case RUN:
|
||||
printf("SYS:runing\n");
|
||||
inter_in_log("SYS:running\n",all_ctx->log_manager);
|
||||
inter_in_log("running\n","SYS",all_ctx->log_manager);
|
||||
return 1;
|
||||
|
||||
case QUIT:
|
||||
printf("SYS:shuting down\n");
|
||||
inter_in_log("SYS:shuting down\n",all_ctx->log_manager);
|
||||
inter_in_log("shuting down\n","SYS",all_ctx->log_manager);
|
||||
all_ctx->statue = -1;
|
||||
write(all_ctx->fifofd[1],"q",1);
|
||||
return 1;
|
||||
|
||||
8
c/main.c
8
c/main.c
@ -3,6 +3,8 @@
|
||||
#include "network/network.h"
|
||||
#include "tools/toml/toml.h"
|
||||
#include "tools/quit/quit.h"
|
||||
#include "memctl/memctl.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -33,9 +35,11 @@ int main()
|
||||
perror("load config error");
|
||||
int port = (int)toml_int_in(server,"list_port").u.i;
|
||||
//加载配置文件,读取端口
|
||||
mem_ctl *mem_ctler = (mem_ctl*)malloc(sizeof(mem_ctl));
|
||||
init_memctl(mem_ctler);
|
||||
log_manager *logsmanager=(log_manager*)malloc(sizeof(log_manager));
|
||||
//创建日志管理器与定时清理线层
|
||||
init_loger(logsmanager);
|
||||
init_loger(logsmanager,mem_ctler);
|
||||
pthread_create(&logsmanager->pid,NULL,logsmanager->clear_log,logsmanager);
|
||||
Ctl *teml = init_tem(logsmanager);
|
||||
teml->config = server;
|
||||
@ -53,13 +57,13 @@ int main()
|
||||
resource->loger = logsmanager;
|
||||
resource->network = networkmanager;
|
||||
resource->tem = teml;
|
||||
resource->memctler = mem_ctler;
|
||||
on_exit(quit_all,resource);
|
||||
//注册清理函数
|
||||
teml->run(teml,fifo);
|
||||
|
||||
//启动终端
|
||||
|
||||
|
||||
//等待网络管理器进程结束
|
||||
pthread_join(networkmanager->pid,NULL);
|
||||
networkmanager->pid = -1;
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
#include "memctl.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define container_of(ptr, type, member) \
|
||||
((type *)((char *)(ptr) - offsetof(type, member)))
|
||||
|
||||
int extend_pool(mem_ctl* self,int size)
|
||||
{
|
||||
@ -20,7 +24,7 @@ int extend_pool(mem_ctl* self,int size)
|
||||
self->blocks[i].location = malloc(MEM_BLOCK_SIZE);
|
||||
if(self->blocks[i].location == NULL)
|
||||
return target-i;//堆内存不足
|
||||
atomic_store(&self->blocks[i].condition,FREE);
|
||||
atomic_store(&self->blocks[i].condition,MEM_FREE);
|
||||
}
|
||||
atomic_fetch_add(&self->poolsize,size);
|
||||
int log = atomic_load(&self->logbuf)+size/2;
|
||||
@ -28,33 +32,41 @@ int extend_pool(mem_ctl* self,int size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
mem_block *GetBlock(mem_ctl* self,int type)
|
||||
void **GetBlock(mem_ctl* self,int type)
|
||||
{
|
||||
int status = 0;
|
||||
int sp=0,start = 0;
|
||||
atomic_int *id;
|
||||
int id;
|
||||
//模式判断
|
||||
if(type == LOGMOD)
|
||||
{
|
||||
id = &self->Loglast_loc;
|
||||
sp = self->poolsize;
|
||||
start = self->logbuf;
|
||||
id = atomic_load(&self->Loglast_loc);
|
||||
sp = atomic_load(&self->poolsize);
|
||||
start = atomic_load(&self->logbuf);
|
||||
if(id<start)
|
||||
id = start;
|
||||
}
|
||||
else
|
||||
{
|
||||
id = &self->Commenlast_loc;
|
||||
sp = self->logbuf;
|
||||
id = atomic_load(&self->Commenlast_loc);
|
||||
sp = atomic_load(&self->logbuf)+1;
|
||||
if(id>sp)
|
||||
id = start;
|
||||
}
|
||||
int i = atomic_load(id);
|
||||
int i = id;
|
||||
for(status;status<CYCLE_NUM;status++)//最多扫描轮次
|
||||
{
|
||||
//获取空闲块
|
||||
for(i;i<sp;i++)
|
||||
{
|
||||
if(atomic_fetch_sub(&self->blocks[i].condition,1) == FREE)
|
||||
if(atomic_fetch_sub(&self->blocks[i].condition,1) == MEM_FREE)
|
||||
{
|
||||
atomic_fetch_sub(&self->blocks[i].condition,1);
|
||||
return &self->blocks[i];
|
||||
if(i>self->logbuf)
|
||||
atomic_store(&self->Loglast_loc,i);
|
||||
else
|
||||
atomic_store(&self->Commenlast_loc,i);
|
||||
return &self->blocks[i].location;
|
||||
}
|
||||
else{
|
||||
atomic_fetch_add(&self->blocks[i].condition,1);//回滚路径
|
||||
@ -74,8 +86,10 @@ mem_block *GetBlock(mem_ctl* self,int type)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int FreeBlock(mem_ctl* self,mem_block *block)
|
||||
int FreeBlock(mem_ctl* self,void** block_p)
|
||||
{
|
||||
mem_block *block;
|
||||
block = container_of(block_p,mem_block,location);
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
if(block == NULL)
|
||||
@ -92,7 +106,7 @@ int FreeBlock(mem_ctl* self,mem_block *block)
|
||||
free(block->location);
|
||||
block->location = NULL;
|
||||
int poolsize = atomic_load(&self->poolsize);
|
||||
if(&self->blocks[poolsize] == block)
|
||||
if(&self->blocks[poolsize-1] == block)
|
||||
{
|
||||
for(int i = poolsize -1;i>COMINE_MEM_SIZE;i--)
|
||||
{
|
||||
@ -136,9 +150,10 @@ int free_memctl(mem_ctl *self)//清除内存池
|
||||
{
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
for(int i = 0;i++;i<MAX_MEM_SIZE)
|
||||
for(int i = 0;i<MAX_MEM_SIZE;i++)
|
||||
{
|
||||
if(self->blocks[i].location!=NULL)
|
||||
free(self->blocks[i].location);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -3,7 +3,7 @@
|
||||
#include "config.h"
|
||||
#include <stdatomic.h>
|
||||
|
||||
#define FREE 1
|
||||
#define MEM_FREE 1
|
||||
#define INUSE -1
|
||||
#define PROCESSING 0
|
||||
|
||||
@ -25,9 +25,9 @@ typedef struct mem_ctl
|
||||
atomic_int Loglast_loc;
|
||||
atomic_int mem_e_indicator;//内存不足指示器
|
||||
//获取一个内存块
|
||||
mem_block* (*GetBlock)(struct mem_ctl*,int);
|
||||
void** (*GetBlock)(struct mem_ctl*,int);
|
||||
//释放一个内存块
|
||||
int (*FreeBlock)(struct memctl*,mem_block*);
|
||||
int (*FreeBlock)(struct mem_ctl*,void**);
|
||||
}mem_ctl;
|
||||
|
||||
int init_memctl(mem_ctl *self);
|
||||
|
||||
@ -160,25 +160,11 @@ typedef struct internal_hooks
|
||||
void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
|
||||
} internal_hooks;
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
/* work around MSVC error C2322: '...' address of dllimport '...' is not static */
|
||||
static void * CJSON_CDECL internal_malloc(size_t size)
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
static void CJSON_CDECL internal_free(void *pointer)
|
||||
{
|
||||
free(pointer);
|
||||
}
|
||||
static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
|
||||
{
|
||||
return realloc(pointer, size);
|
||||
}
|
||||
#else
|
||||
|
||||
#define internal_malloc malloc
|
||||
#define internal_free free
|
||||
#define internal_realloc realloc
|
||||
#endif
|
||||
|
||||
|
||||
/* strlen of character literals resolved at compile time */
|
||||
#define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
|
||||
|
||||
@ -13,64 +13,10 @@
|
||||
|
||||
|
||||
int write_in_bk(char input,httpbuf* blk){
|
||||
if(blk->size%HTTP_BLOCK_SIZE == 0)//块满分配新块
|
||||
{
|
||||
void *tes = realloc(blk->buf,blk->size+HTTP_BLOCK_SIZE);
|
||||
if(tes = NULL){//无法分配内存,清理并返回NULL
|
||||
free(blk->buf);
|
||||
perror("http rec error");
|
||||
return -1;
|
||||
}
|
||||
blk->buf = tes;
|
||||
}
|
||||
blk->buf[blk->size] = input;
|
||||
blk->size++;
|
||||
return 0;
|
||||
|
||||
}
|
||||
char *recv_http_request(int cfd){
|
||||
httpbuf buf;
|
||||
buf.buf = NULL;
|
||||
// cppcheck-suppress missingReturn
|
||||
buf.buf = (char*)malloc(HTTP_BLOCK_SIZE);
|
||||
char input;
|
||||
for(;;)
|
||||
{
|
||||
read(cfd,&input,1);
|
||||
switch(input){//截取头部
|
||||
case '\n':
|
||||
if(buf.buf[buf.size-1] == '\r'&&buf.buf[buf.size-2] =='\n'&&buf.buf[buf.size-3] == '\r')
|
||||
{
|
||||
buf.size-3;
|
||||
buf.buf = (char*)realloc(buf.buf,buf.size);//截取头部
|
||||
goto HEAD_RCV;//跳转到头部解析
|
||||
}
|
||||
else
|
||||
{
|
||||
if(write_in_bk(input,&buf)==-1)
|
||||
abort();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if(write_in_bk(input,&buf)==-1)
|
||||
abort();//缓存头部
|
||||
break;
|
||||
}
|
||||
}
|
||||
HEAD_RCV:
|
||||
//获取http体长度
|
||||
char length[MAX_HTTP_LENGTH] = {' '};
|
||||
int index = buf.size-1;
|
||||
int tobewrite = MAX_HTTP_LENGTH-1;
|
||||
while(buf.buf[index]!=' ')
|
||||
{
|
||||
length[tobewrite] = buf.buf[index];
|
||||
index--;tobewrite--;
|
||||
}
|
||||
int bodylength = atoi(length);
|
||||
free(buf.buf);//释放头部
|
||||
buf.buf = malloc(bodylength);
|
||||
read(cfd,buf.buf,bodylength);
|
||||
return buf.buf;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -82,49 +28,48 @@ char *recv_http_request(int cfd){
|
||||
*/
|
||||
int init_http_network(int port, log_manager *logger)
|
||||
{
|
||||
logs *log;
|
||||
|
||||
int fd;
|
||||
|
||||
char log[MAX_LOG_LENGTH];
|
||||
/* 1. 创建socket */
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
// cppcheck-suppress uninitdata
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] socket() failed: %s", strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
snprintf(log, sizeof(log),
|
||||
"socket() failed: %s", strerror(errno));
|
||||
logger->in_log(logger, log,"FATAL");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 2. 设置SO_REUSEADDR,避免TIME_WAIT状态导致bind失败 */
|
||||
int opt = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[ERROR] setsockopt(SO_REUSEADDR) on fd=%d failed: %s",
|
||||
snprintf(log, sizeof(log),
|
||||
"setsockopt(SO_REUSEADDR) on fd=%d failed: %s",
|
||||
fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 3. 设置为非阻塞模式(配合epoll使用) */
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[ERROR] fcntl(F_GETFL) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
|
||||
|
||||
snprintf(log, sizeof(log),
|
||||
"fcntl(F_GETFL) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[ERROR] fcntl(O_NONBLOCK) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
snprintf(log, sizeof(log),
|
||||
"fcntl(O_NONBLOCK) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -135,30 +80,29 @@ int init_http_network(int port, log_manager *logger)
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 监听所有网卡
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] bind(port %d) failed: %s (fd=%d)",
|
||||
snprintf(log, sizeof(log),
|
||||
"bind(port %d) failed: %s (fd=%d)",
|
||||
port, strerror(errno), fd);
|
||||
logger->in_log(log, logger);
|
||||
logger->in_log(logger,log,"FATAL:");
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 5. 开始监听 */
|
||||
if (listen(fd, 10) == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] listen(fd=%d, backlog=10) failed: %s",
|
||||
snprintf(log, sizeof(log),
|
||||
"listen(fd=%d, backlog=10) failed: %s",
|
||||
fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
logger->in_log(logger,log,"FATAL:");
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 6. 成功日志 */
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[HTTP] Successfully listening on port %d (fd=%d)", port, fd);
|
||||
logger->in_log(log, logger);
|
||||
snprintf(log, sizeof(log),
|
||||
"Successfully listening on port %d (fd=%d)", port, fd);
|
||||
logger->in_log(logger, log,"HTTP:");
|
||||
return fd;
|
||||
}
|
||||
|
||||
@ -2,10 +2,11 @@
|
||||
#define HTTP_REL
|
||||
|
||||
#include "config.h"
|
||||
#include "memctl/memctl.h"
|
||||
|
||||
typedef struct httpbuf{
|
||||
char *buf;
|
||||
int size;
|
||||
char buf[MEM_BLOCK_SIZE-5];
|
||||
}httpbuf;//http分块结构体
|
||||
|
||||
char *recv_http_request(int cfd);
|
||||
|
||||
@ -103,12 +103,12 @@ int process_message(char *req, log_manager *logger,rbt_msg *swap) {
|
||||
const char *body = recv_http_request(fd);
|
||||
|
||||
if(rbt_parse_json(body,swap) == 0) {
|
||||
logs *log = malloc(sizeof(logs));
|
||||
char log[MAX_LOG_LENGTH];
|
||||
// cppcheck-suppress uninitdata
|
||||
snprintf(log->log, sizeof(log->log), "%s message %s processed ok\n",
|
||||
snprintf(log, sizeof(log), "%s message %s processed ok\n",
|
||||
swap->nickname,swap->raw_message);
|
||||
make_swap(swap);
|
||||
logger->in_log(log, logger);
|
||||
logger->in_log(logger,log,"PROCESSER:");
|
||||
}
|
||||
//通知前端已收到消息
|
||||
const char *response =
|
||||
@ -165,11 +165,11 @@ void *pth_module(void *args_p)
|
||||
NULL};
|
||||
execv("Run_pluhginmanager",args);
|
||||
}
|
||||
logs *pth_log = (logs*)malloc(sizeof(logs));
|
||||
char pth_log[40];
|
||||
// cppcheck-suppress uninitdata
|
||||
sprintf(pth_log->log,"PID:%lu launched python plugines\n",pthread_self());
|
||||
sprintf(pth_log,"launched python plugines,pid:%ld\n",pthread_self());
|
||||
|
||||
logger->in_log(pth_log,logger);
|
||||
logger->in_log(logger,pth_log,"PROCESSER:");
|
||||
rbt_msg *swap = (rbt_msg*)mmap(NULL, sizeof(rbt_msg), PROT_READ|PROT_WRITE, MAP_SHARED,swapfd, 0);
|
||||
//拉起python插件管理器
|
||||
for(;;){
|
||||
@ -185,9 +185,6 @@ void *pth_module(void *args_p)
|
||||
break;
|
||||
}
|
||||
else{
|
||||
pth_log = (logs*)malloc(sizeof(logs));
|
||||
sprintf(pth_log->log,"processd message");
|
||||
logger->in_log(pth_log,logger);
|
||||
process_message(req,logger,swap);
|
||||
atomic_fetch_add(&pmd->status, 1);
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ int create_swap(const char *name)
|
||||
memcpy(init_msg->nickname,buf,64);
|
||||
sem_init(&init_msg->status,1,1);
|
||||
init_msg->raw_message[0] = '\0';
|
||||
init_msg->state = FREE;
|
||||
init_msg->state = MEM_FREE;
|
||||
init_msg->uid[0] = '\0';
|
||||
munmap((void*)init_msg,sizeof(rbt_msg));
|
||||
return fd;
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
#include "config.h"
|
||||
#define QUITPLG 0
|
||||
#define NEWMSG 1
|
||||
#define FREE 2
|
||||
#define SW_FREE 2
|
||||
|
||||
int make_swap(rbt_msg *swap);
|
||||
int create_swap(const char *name);
|
||||
|
||||
@ -252,9 +252,8 @@ int teml(Ctl *self,int fifo[2])
|
||||
perror("sys error");
|
||||
//将用户输入入队
|
||||
infifo(self,input);
|
||||
logs *log = (logs*)malloc(sizeof(logs));
|
||||
memcpy(log->log,input,sizeof(input));
|
||||
self->logmanager->in_log(log,self->logmanager);
|
||||
|
||||
self->logmanager->in_log(self->logmanager,input,"USER:");
|
||||
memcpy(command->command,input,sizeof(input));
|
||||
interpret(SIG_MOD,command,cmd_dir);
|
||||
const char fexp[256] = {'\0'};
|
||||
|
||||
@ -9,7 +9,12 @@
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
int write_into_block(char *writein,char *org,int *length,int maxlength,char *logname)
|
||||
|
||||
logs* getbody(void **log_p)
|
||||
{
|
||||
return (logs*)*log_p;
|
||||
}
|
||||
int write_into_block(char *writein,char *org,size_t* length,size_t maxlength,char *logname)
|
||||
{
|
||||
if(writein == NULL||org == NULL||length == NULL||logname == NULL)
|
||||
return -1;
|
||||
@ -18,69 +23,63 @@ int write_into_block(char *writein,char *org,int *length,int maxlength,char *log
|
||||
strcpy(&writein[*length],org);
|
||||
*length +=strlen(org);//栈内存充足
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
int n = *length + strlen(org) - maxlength;
|
||||
int n = *length + strlen(org) - maxlength+1;
|
||||
strncpy(&writein[*length],org,strlen(org)-n);//栈内存不足
|
||||
writein[maxlength-1] = '\0';
|
||||
int fd = open(logname,O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd != -1)
|
||||
{
|
||||
write(fd,writein,maxlength);
|
||||
int eno = write(fd,writein,strlen(writein));
|
||||
if(eno <strlen(writein))
|
||||
perror("log");
|
||||
close(fd);
|
||||
}
|
||||
else if(fd == -1)
|
||||
perror("log:");//仅警告
|
||||
strncpy(writein,&org[0],n);//剩余部分拷贝
|
||||
*length = n;
|
||||
strcpy(writein,&org[*length]);//剩余部分拷贝
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int in_log(logs *log,log_manager *self)
|
||||
int in_log(log_manager *self,const char *logbody,const char *info)
|
||||
{
|
||||
if(log == NULL||self == NULL)
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
void **log_p = self->mempool->GetBlock(self->mempool,LOGMOD);
|
||||
if(log_p == NULL)
|
||||
{
|
||||
perror("Mem_runout");
|
||||
return -1;
|
||||
}
|
||||
logs *log = getbody(log_p);
|
||||
snprintf(log->info,INFO_LENGTH,"%s",info);
|
||||
snprintf(log->log,MAX_LOG_LENGTH,"%s",logbody);
|
||||
log->log[MAX_LOG_LENGTH-1] = '\0';
|
||||
log->next = NULL;
|
||||
sem_wait(&self->log_sem);//加锁
|
||||
if(self->log == NULL){
|
||||
self->log = log;
|
||||
self->rear = log;
|
||||
self->count++;
|
||||
self->log = log_p;
|
||||
self->rear = getbody(log_p);
|
||||
atomic_fetch_add(&self->count,1);
|
||||
sem_post(&self->log_sem);
|
||||
return self->count;
|
||||
}
|
||||
if(self->count == 1)
|
||||
self->log->next = log;
|
||||
if(self->count == 1){
|
||||
logs *p = getbody(self->log);
|
||||
p->next = log_p;
|
||||
}
|
||||
self->count++;
|
||||
log->next = NULL;
|
||||
self->rear->next = log;
|
||||
self->rear->next = log_p;
|
||||
self->rear = log;
|
||||
sem_post(&self->log_sem);
|
||||
return self->count;
|
||||
}
|
||||
|
||||
logs *out_log(log_manager *self)
|
||||
{
|
||||
if(self == NULL)
|
||||
return NULL;
|
||||
sem_wait(&self->log_sem);
|
||||
logs *buf = self->log;
|
||||
if(buf==NULL)
|
||||
{
|
||||
sem_post(&self->log_sem);
|
||||
return NULL;
|
||||
}
|
||||
if(self->log->next ==NULL)
|
||||
self->log = self->rear = NULL;
|
||||
else
|
||||
self->log = self->log->next;
|
||||
self->count--;
|
||||
sem_post(&self->log_sem);
|
||||
buf->next =NULL;
|
||||
return buf;
|
||||
}
|
||||
|
||||
int sleep_with_signal(log_manager *self)
|
||||
{
|
||||
struct timespec ts;
|
||||
@ -90,7 +89,7 @@ int sleep_with_signal(log_manager *self)
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
|
||||
return -1; /* 罕见失败 */
|
||||
|
||||
ts.tv_sec += 1000;
|
||||
ts.tv_sec += LOG_SLEEP_LENGTH;
|
||||
/* 纳秒部分无需处理,1000 s 整不会溢出 */
|
||||
|
||||
pthread_mutex_lock(&self->mtx); /* 进入临界区 */
|
||||
@ -119,92 +118,55 @@ int cleanup(log_manager *self)
|
||||
if(self->log ==NULL)
|
||||
return 1;
|
||||
logs *tobeclean,*loc;
|
||||
void **tobeclean_p;
|
||||
sem_wait(&self->log_sem);//获取信号量
|
||||
loc = self->log;
|
||||
void **loc_p = self->log;
|
||||
|
||||
self->log = NULL;
|
||||
self->count = 0;//摘取log链
|
||||
atomic_store(&self->count,0);//摘取log链
|
||||
sem_post(&self->log_sem);
|
||||
//释放信号量
|
||||
char *logbuf;
|
||||
void *logbufbk;
|
||||
char failback[MAX_LOG_LENGTH];
|
||||
logbuf = (char*)malloc(1);
|
||||
logbuf[0] = '\0';
|
||||
size_t buf_length = 0,buf_lengthbk = 0;
|
||||
loc = getbody(loc_p);
|
||||
|
||||
char logbuf[MAX_LOG_LENGTH];
|
||||
|
||||
size_t buf_length = 0;
|
||||
int fd;
|
||||
while(loc->next !=NULL)
|
||||
{
|
||||
tobeclean = loc;
|
||||
loc = loc->next;
|
||||
if(logbuf != failback){
|
||||
logbufbk = logbuf;
|
||||
logbuf = (char*)realloc(logbuf,strlen(logbuf)+strlen(tobeclean->log)+1);//为日志分配新的内存
|
||||
}
|
||||
if(logbuf == NULL){
|
||||
fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd != -1)
|
||||
{
|
||||
write(fd,logbufbk,buf_length);
|
||||
close(fd);
|
||||
}
|
||||
free(logbufbk);
|
||||
logbuf = failback;//降级策略,堆空间不足时,使用预分配栈空间
|
||||
buf_length = 0;
|
||||
}
|
||||
if(logbuf != failback){
|
||||
buf_lengthbk = buf_length;
|
||||
buf_length = strlen(logbuf)+strlen(tobeclean->log);
|
||||
strcpy(logbuf+buf_lengthbk,tobeclean->log);
|
||||
}
|
||||
else
|
||||
{
|
||||
write_into_block(logbuf,tobeclean->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
}
|
||||
free(tobeclean);
|
||||
}
|
||||
if(logbuf != failback){
|
||||
logbufbk = logbuf;
|
||||
logbuf = (char*)realloc(logbuf,strlen(logbuf)+strlen(loc->log)+2);
|
||||
if(logbuf == NULL)
|
||||
{
|
||||
free(logbufbk);
|
||||
logbuf = failback;
|
||||
strcpy(logbuf,loc->log);
|
||||
buf_length = strlen(loc->log);
|
||||
|
||||
}
|
||||
else{
|
||||
buf_lengthbk = buf_length;
|
||||
buf_length = strlen(logbuf)+strlen(loc->log);
|
||||
strcpy(logbuf+buf_lengthbk,loc->log);
|
||||
}
|
||||
}
|
||||
else{
|
||||
write_into_block(logbuf,loc->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
}
|
||||
tobeclean_p = loc_p;
|
||||
tobeclean = getbody(tobeclean_p);
|
||||
loc_p = loc->next;
|
||||
loc = getbody(loc_p);
|
||||
|
||||
int eno = write_into_block(logbuf,tobeclean->info,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
if(eno == -1)
|
||||
perror("log");
|
||||
eno = write_into_block(logbuf,":",&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
eno = write_into_block(logbuf,tobeclean->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
if(eno == -1)
|
||||
perror("log");//非业务逻辑只警告
|
||||
|
||||
logbuf[buf_length] = '\0';
|
||||
self->mempool->FreeBlock(self->mempool,tobeclean_p);
|
||||
}
|
||||
write_into_block(logbuf,loc->info,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
write_into_block(logbuf,":",&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
write_into_block(logbuf,loc->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
|
||||
free(loc);
|
||||
|
||||
fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd == -1){
|
||||
perror("file:");
|
||||
if(logbuf != failback)
|
||||
free(logbuf);
|
||||
return -1;
|
||||
perror("log:");
|
||||
}
|
||||
int error_buf = write(fd,logbuf,strlen(logbuf));
|
||||
if(error_buf==-1){
|
||||
close(fd);
|
||||
if(logbuf != failback)
|
||||
free(logbuf);
|
||||
return -1;
|
||||
}
|
||||
else if(error_buf<buf_length)
|
||||
else if(error_buf<strlen(logbuf)){
|
||||
perror("file");
|
||||
write(fd,"unknown error case log write cut down\n",38);
|
||||
if(logbuf != failback)
|
||||
free(logbuf);
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
@ -219,13 +181,9 @@ void log_manager_stop(log_manager *self)
|
||||
self->stop = 1;
|
||||
/* 置退出标志 */
|
||||
printf("SYS:stopping loger\n");
|
||||
logs *log = malloc(sizeof(logs));
|
||||
strcpy(log->log,"SYS:stopping loger\n");
|
||||
self->in_log(log,self);
|
||||
log = malloc(sizeof(logs));
|
||||
strcpy(log->log,"SYS:done\n");
|
||||
self->in_log(self,"stopping loger\n","SYS:");
|
||||
printf("SYS:done\n");
|
||||
self->in_log(log,self);
|
||||
self->in_log(self,"done","SYS:");
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
|
||||
}
|
||||
@ -250,7 +208,7 @@ void *clear_log(void *self_p)
|
||||
}
|
||||
}
|
||||
|
||||
int init_loger(log_manager *self)
|
||||
int init_loger(log_manager *self,mem_ctl *mempool)
|
||||
{
|
||||
if(self == NULL)
|
||||
{
|
||||
@ -280,11 +238,11 @@ int init_loger(log_manager *self)
|
||||
return -1;
|
||||
}
|
||||
self->in_log = in_log;
|
||||
self->out_log = out_log;
|
||||
self->clear_log = clear_log;
|
||||
self->log = NULL;
|
||||
self->stop = 0;
|
||||
self->cleanup = cleanup;
|
||||
self->count = 0;
|
||||
atomic_init(&self->count,0);
|
||||
self->mempool = mempool;
|
||||
return 0;
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
#define LOG
|
||||
|
||||
#include "config.h"
|
||||
#include "memctl/memctl.h"
|
||||
#include <semaphore.h>
|
||||
#include <pthread.h>
|
||||
|
||||
@ -9,27 +10,27 @@
|
||||
typedef struct logs
|
||||
{
|
||||
char log[MAX_LOG_LENGTH];
|
||||
struct logs *next;
|
||||
void **next;
|
||||
char info[INFO_LENGTH];
|
||||
}logs;
|
||||
|
||||
typedef struct log_manager
|
||||
{
|
||||
pthread_t pid;
|
||||
int (*in_log)(logs *,struct log_manager*);
|
||||
logs* (*out_log)(struct log_manager*);
|
||||
mem_ctl *mempool;
|
||||
int (*in_log)(struct log_manager*,const char *,const char *);
|
||||
void *(*clear_log)(void*);
|
||||
int (*cleanup)(struct log_manager*);
|
||||
sem_t log_sem;
|
||||
logs *log;
|
||||
void **log;
|
||||
logs *rear;
|
||||
int count;
|
||||
atomic_int count;
|
||||
pthread_mutex_t mtx;
|
||||
pthread_cond_t cond;
|
||||
int stop;
|
||||
}log_manager;
|
||||
|
||||
void log_manager_stop(log_manager *self);
|
||||
int init_loger(log_manager *self);
|
||||
int init_loger(log_manager *self,mem_ctl *mempool);
|
||||
|
||||
#endif
|
||||
@ -11,6 +11,7 @@
|
||||
#include "tem/ctl.h"
|
||||
#include "tools/toml/toml.h"
|
||||
|
||||
|
||||
int quit_server(netm *self)
|
||||
{
|
||||
if(self ==NULL)
|
||||
@ -44,15 +45,18 @@ int quit_server(netm *self)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int quit_mempool(mem_ctl *mem_ctler)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void quit_all(int status,void *self_p)
|
||||
{
|
||||
alres *resouce =(alres*)self_p;
|
||||
//转换参数
|
||||
|
||||
resouce->network->shutdown_pool(resouce->network);
|
||||
logs *netlog = (logs*)malloc(sizeof(logs));
|
||||
netlog->next = NULL;
|
||||
memcpy(netlog->log,"shuting down networkserver",27);
|
||||
|
||||
|
||||
if(resouce->network->statue == SERVER_ON)
|
||||
{
|
||||
@ -62,7 +66,7 @@ void quit_all(int status,void *self_p)
|
||||
{
|
||||
resouce->network->shutdown_pool(resouce->network);
|
||||
}
|
||||
resouce->loger->in_log(netlog,resouce->loger);
|
||||
resouce->loger->in_log(resouce->loger,"shutting down network pool","SYS:");
|
||||
free(resouce->network);
|
||||
//释放网络资源
|
||||
if(resouce->tem->command !=NULL){
|
||||
@ -97,4 +101,5 @@ void quit_all(int status,void *self_p)
|
||||
|
||||
free(resouce->loger);
|
||||
//清理日志
|
||||
free(resouce);
|
||||
}
|
||||
|
||||
@ -4,11 +4,14 @@
|
||||
#include "network/network.h"
|
||||
#include "tem/ctl.h"
|
||||
#include "tools/log/log.h"
|
||||
#include "memctl/memctl.h"
|
||||
|
||||
typedef struct all_resources
|
||||
{
|
||||
Ctl *tem;
|
||||
netm *network;
|
||||
log_manager *loger;
|
||||
mem_ctl *memctler;
|
||||
|
||||
}alres;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user