log管理器内存分配池化,修复部分log写入部分与内存池部分存在的恶性bug

This commit is contained in:
2026-02-21 15:04:04 +08:00
parent b618cc359a
commit d81a3c8042
16 changed files with 189 additions and 277 deletions

View File

@ -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(""))

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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);