Compare commits
8 Commits
4445567169
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 02a9d73817 | |||
| d81a3c8042 | |||
| b618cc359a | |||
| b23828cd5c | |||
| bdf8ef62f7 | |||
| a8c5f0dabf | |||
| 19e0392db6 | |||
| d01219da30 |
2
.gitignore
vendored
Normal file → Executable file
2
.gitignore
vendored
Normal file → Executable file
@ -6,7 +6,7 @@ __pycache__/
|
|||||||
|
|
||||||
# C extensions
|
# C extensions
|
||||||
*.so
|
*.so
|
||||||
|
.vscode/
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
.Python
|
.Python
|
||||||
build/
|
build/
|
||||||
|
|||||||
3
c/CMakeLists.txt
Normal file → Executable file
3
c/CMakeLists.txt
Normal file → Executable file
@ -17,7 +17,8 @@ add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstal
|
|||||||
add_library(Log SHARED tools/log/log.c)
|
add_library(Log SHARED tools/log/log.c)
|
||||||
add_library(Toml SHARED tools/toml/toml.c)
|
add_library(Toml SHARED tools/toml/toml.c)
|
||||||
add_library(Quit SHARED tools/quit/quit.c)
|
add_library(Quit SHARED tools/quit/quit.c)
|
||||||
|
add_library(Memctl SHARED memctl/memctl.c)
|
||||||
|
|
||||||
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit)
|
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit Memctl)
|
||||||
|
|
||||||
include_directories(${PROJECT_SOURCE_DIR})
|
include_directories(${PROJECT_SOURCE_DIR})
|
||||||
23
c/config.h
Normal file → Executable file
23
c/config.h
Normal file → Executable file
@ -1,18 +1,37 @@
|
|||||||
#ifndef SEVERCONFG
|
#ifndef SEVERCONFG
|
||||||
#define SEVERCONFG
|
#define SEVERCONFG
|
||||||
|
|
||||||
#define MAX_LOG 64
|
/*------日志管理---------*/
|
||||||
#define MAX_LOG_LENGTH 4096
|
#define MAX_LOG 50
|
||||||
|
#define MAX_LOG_LENGTH 4080
|
||||||
|
#define INFO_LENGTH 8
|
||||||
|
#define LOG_SLEEP_LENGTH 1
|
||||||
|
/*------日志管理---------*/
|
||||||
|
|
||||||
|
/*-------终端管理-------*/
|
||||||
#define TEM_MAX_BUF 256
|
#define TEM_MAX_BUF 256
|
||||||
#define TEM_HISTORY_BUF 210
|
#define TEM_HISTORY_BUF 210
|
||||||
#define TEM_PROMPT "chatbot$$ "
|
#define TEM_PROMPT "chatbot$$ "
|
||||||
|
/*----终端管理--------*/
|
||||||
|
|
||||||
|
/*-------网路池管理-----*/
|
||||||
#define NET_MAX_POOL 1
|
#define NET_MAX_POOL 1
|
||||||
#define NET_MAX_MESSAGE_BUF 1024
|
#define NET_MAX_MESSAGE_BUF 1024
|
||||||
#define HTTP_BLOCK_SIZE 512
|
#define HTTP_BLOCK_SIZE 512
|
||||||
#define MAX_HTTP_LENGTH 20
|
#define MAX_HTTP_LENGTH 20
|
||||||
|
/*-------网路池管理-----*/
|
||||||
|
|
||||||
|
/*------解释器管理-------*/
|
||||||
#define INTER_MAX_BUF 256
|
#define INTER_MAX_BUF 256
|
||||||
|
/*------解释器管理-------*/
|
||||||
|
|
||||||
|
/*------内存池管理------*/
|
||||||
|
#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
|
||||||
|
#define CYCLE_NUM 7
|
||||||
|
/*------内存池管理------*/
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
30
c/interpreter/interpreter.c
Normal file → Executable file
30
c/interpreter/interpreter.c
Normal file → Executable file
@ -9,24 +9,22 @@
|
|||||||
#include "interpreter.h"
|
#include "interpreter.h"
|
||||||
#include "tools/pkgmanager/pkginstall.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)
|
if(strlen(log)>1024)
|
||||||
return -1;
|
return -1;
|
||||||
logs* logss = (logs*)malloc(sizeof(logs));
|
manager->in_log(manager,log,info);
|
||||||
memcpy(logss->log,log,strlen(log));
|
|
||||||
manager->in_log(logss,manager);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager)
|
int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager)
|
||||||
{
|
{
|
||||||
printf("SYS:prepare env\n");
|
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");
|
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");
|
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");
|
sprintf(cmd_dic[0].name, "pkginstall");
|
||||||
cmd_dic[0].cmd = INSTALL;
|
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;
|
cmd_dic[2].cmd = QUIT;
|
||||||
|
|
||||||
printf("SYS:cmd_dir load complite\n");
|
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++)
|
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;
|
self->arg = NULL;
|
||||||
printf("SYS:Creating ctl fifo\n");
|
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));
|
memcpy(self->fifofd,fifo,2*sizeof(int));
|
||||||
self->log_manager = log_manager;
|
self->log_manager = log_manager;
|
||||||
}
|
}
|
||||||
@ -71,7 +69,7 @@ int get_args(ctx *self)
|
|||||||
arg->next = (args*)malloc(sizeof(args));
|
arg->next = (args*)malloc(sizeof(args));
|
||||||
if(arg->next == NULL){
|
if(arg->next == NULL){
|
||||||
perror("ERROR:fail to get mem");
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
arg = arg->next;
|
arg = arg->next;
|
||||||
@ -146,31 +144,31 @@ int exec(const int command,ctx *all_ctx)
|
|||||||
{
|
{
|
||||||
case BAD_INPUT:
|
case BAD_INPUT:
|
||||||
printf("SYS:bad input,try again\n");
|
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;
|
return BAD_INPUT;
|
||||||
|
|
||||||
case INSTALL:
|
case INSTALL:
|
||||||
if(all_ctx->arg == NULL){
|
if(all_ctx->arg == NULL){
|
||||||
printf("SYS:Missing args\n");
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
printf("SYS:init pkgmanager\n");
|
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();
|
pkger *manager = init_pkginstaller();
|
||||||
printf("SYS:installing\n");
|
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);
|
manager->packup(manager);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
case RUN:
|
case RUN:
|
||||||
printf("SYS:runing\n");
|
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;
|
return 1;
|
||||||
|
|
||||||
case QUIT:
|
case QUIT:
|
||||||
printf("SYS:shuting down\n");
|
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;
|
all_ctx->statue = -1;
|
||||||
write(all_ctx->fifofd[1],"q",1);
|
write(all_ctx->fifofd[1],"q",1);
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
0
c/interpreter/interpreter.h
Normal file → Executable file
0
c/interpreter/interpreter.h
Normal file → Executable file
8
c/main.c
Normal file → Executable file
8
c/main.c
Normal file → Executable file
@ -3,6 +3,8 @@
|
|||||||
#include "network/network.h"
|
#include "network/network.h"
|
||||||
#include "tools/toml/toml.h"
|
#include "tools/toml/toml.h"
|
||||||
#include "tools/quit/quit.h"
|
#include "tools/quit/quit.h"
|
||||||
|
#include "memctl/memctl.h"
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -33,9 +35,11 @@ int main()
|
|||||||
perror("load config error");
|
perror("load config error");
|
||||||
int port = (int)toml_int_in(server,"list_port").u.i;
|
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));
|
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);
|
pthread_create(&logsmanager->pid,NULL,logsmanager->clear_log,logsmanager);
|
||||||
Ctl *teml = init_tem(logsmanager);
|
Ctl *teml = init_tem(logsmanager);
|
||||||
teml->config = server;
|
teml->config = server;
|
||||||
@ -53,13 +57,13 @@ int main()
|
|||||||
resource->loger = logsmanager;
|
resource->loger = logsmanager;
|
||||||
resource->network = networkmanager;
|
resource->network = networkmanager;
|
||||||
resource->tem = teml;
|
resource->tem = teml;
|
||||||
|
resource->memctler = mem_ctler;
|
||||||
on_exit(quit_all,resource);
|
on_exit(quit_all,resource);
|
||||||
//注册清理函数
|
//注册清理函数
|
||||||
teml->run(teml,fifo);
|
teml->run(teml,fifo);
|
||||||
|
|
||||||
//启动终端
|
//启动终端
|
||||||
|
|
||||||
|
|
||||||
//等待网络管理器进程结束
|
//等待网络管理器进程结束
|
||||||
pthread_join(networkmanager->pid,NULL);
|
pthread_join(networkmanager->pid,NULL);
|
||||||
networkmanager->pid = -1;
|
networkmanager->pid = -1;
|
||||||
|
|||||||
159
c/memctl/memctl.c
Normal file
159
c/memctl/memctl.c
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
#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)
|
||||||
|
{
|
||||||
|
if(self == NULL)//入参检查
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int poolsize = atomic_load(&self->poolsize);
|
||||||
|
int target = poolsize +size;//计算目标大小
|
||||||
|
if(target >MAX_MEM_SIZE)
|
||||||
|
target = MAX_MEM_SIZE;//计算限制
|
||||||
|
|
||||||
|
for(int i = poolsize;i<target;i++)//分配内存
|
||||||
|
{
|
||||||
|
if(self->blocks[i].location !=NULL)//若存在未释放内存
|
||||||
|
free(self->blocks[i].location);
|
||||||
|
self->blocks[i].location = malloc(MEM_BLOCK_SIZE);
|
||||||
|
if(self->blocks[i].location == NULL)
|
||||||
|
return target-i;//堆内存不足
|
||||||
|
atomic_store(&self->blocks[i].condition,MEM_FREE);
|
||||||
|
}
|
||||||
|
atomic_fetch_add(&self->poolsize,size);
|
||||||
|
int log = atomic_load(&self->logbuf)+size/2;
|
||||||
|
atomic_store(&self->logbuf,log);//重新划定日志区
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void **GetBlock(mem_ctl* self,int type)
|
||||||
|
{
|
||||||
|
int status = 0;
|
||||||
|
int sp=0,start = 0;
|
||||||
|
int id;
|
||||||
|
//模式判断
|
||||||
|
if(type == LOGMOD)
|
||||||
|
{
|
||||||
|
id = atomic_load(&self->Loglast_loc);
|
||||||
|
sp = atomic_load(&self->poolsize);
|
||||||
|
start = atomic_load(&self->logbuf);
|
||||||
|
if(id<start)
|
||||||
|
id = start;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
id = atomic_load(&self->Commenlast_loc);
|
||||||
|
sp = atomic_load(&self->logbuf)+1;
|
||||||
|
if(id>sp)
|
||||||
|
id = start;
|
||||||
|
}
|
||||||
|
int i = id;
|
||||||
|
for(status;status<CYCLE_NUM;status++)//最多扫描轮次
|
||||||
|
{
|
||||||
|
//获取空闲块
|
||||||
|
for(i;i<sp;i++)
|
||||||
|
{
|
||||||
|
if(atomic_fetch_sub(&self->blocks[i].condition,1) == MEM_FREE)
|
||||||
|
{
|
||||||
|
atomic_fetch_sub(&self->blocks[i].condition,1);
|
||||||
|
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);//回滚路径
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = start;
|
||||||
|
atomic_fetch_add(&self->mem_e_indicator,1);
|
||||||
|
//检查是否需要扩池
|
||||||
|
if(atomic_fetch_sub(&self->mem_e_indicator,POOL_EXPEND_ID)>POOL_EXPEND_ID)
|
||||||
|
{
|
||||||
|
extend_pool(self,POOL_EXPEND_SIZE);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
atomic_fetch_add(&self->mem_e_indicator,POOL_EXPEND_ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
return -1;
|
||||||
|
if(block < &self->blocks[COMINE_MEM_SIZE])//标准池部分归还
|
||||||
|
{
|
||||||
|
atomic_fetch_add(&block->condition,2);
|
||||||
|
block = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else//扩容池尝试回收
|
||||||
|
{
|
||||||
|
atomic_fetch_add(&block->condition,1);
|
||||||
|
free(block->location);
|
||||||
|
block->location = NULL;
|
||||||
|
int poolsize = atomic_load(&self->poolsize);
|
||||||
|
if(&self->blocks[poolsize-1] == block)
|
||||||
|
{
|
||||||
|
for(int i = poolsize -1;i>COMINE_MEM_SIZE;i--)
|
||||||
|
{
|
||||||
|
if(atomic_load(&self->blocks[i].condition) == PROCESSING)
|
||||||
|
{
|
||||||
|
poolsize--;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(poolsize-COMINE_MEM_SIZE%2 == 0)
|
||||||
|
{
|
||||||
|
atomic_fetch_sub(&self->logbuf,1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
atomic_store(&self->poolsize,poolsize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int init_memctl(mem_ctl *self)
|
||||||
|
{
|
||||||
|
if(self == NULL)
|
||||||
|
return -1;
|
||||||
|
self->poolsize = 0;
|
||||||
|
for(int i =0;i<MAX_MEM_SIZE;i++)
|
||||||
|
{
|
||||||
|
self->blocks[i].location = NULL;
|
||||||
|
atomic_init(&self->blocks[i].condition,PROCESSING);//初始化池
|
||||||
|
}
|
||||||
|
extend_pool(self,COMINE_MEM_SIZE);//预分配内存
|
||||||
|
self->Commenlast_loc = 0;
|
||||||
|
self->logbuf = COMINE_MEM_SIZE/2;
|
||||||
|
self->mem_e_indicator = 0;
|
||||||
|
self->FreeBlock = FreeBlock;
|
||||||
|
self->GetBlock = GetBlock;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int free_memctl(mem_ctl *self)//清除内存池
|
||||||
|
{
|
||||||
|
if(self == NULL)
|
||||||
|
return -1;
|
||||||
|
for(int i = 0;i<MAX_MEM_SIZE;i++)
|
||||||
|
{
|
||||||
|
if(self->blocks[i].location!=NULL)
|
||||||
|
free(self->blocks[i].location);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
36
c/memctl/memctl.h
Normal file
36
c/memctl/memctl.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#ifndef MEMCTL
|
||||||
|
#define MEMCTL
|
||||||
|
#include "config.h"
|
||||||
|
#include <stdatomic.h>
|
||||||
|
|
||||||
|
#define MEM_FREE 1
|
||||||
|
#define INUSE -1
|
||||||
|
#define PROCESSING 0
|
||||||
|
|
||||||
|
#define LOGMOD 0
|
||||||
|
#define COMMENMOD 1
|
||||||
|
|
||||||
|
typedef struct mem_block
|
||||||
|
{
|
||||||
|
void *location;//块地址
|
||||||
|
atomic_int condition;//块状态
|
||||||
|
}mem_block;
|
||||||
|
|
||||||
|
typedef struct mem_ctl
|
||||||
|
{
|
||||||
|
mem_block blocks[MAX_MEM_SIZE];
|
||||||
|
atomic_int logbuf;
|
||||||
|
atomic_int poolsize;
|
||||||
|
atomic_int Commenlast_loc;//分配起始
|
||||||
|
atomic_int Loglast_loc;
|
||||||
|
atomic_int mem_e_indicator;//内存不足指示器
|
||||||
|
//获取一个内存块
|
||||||
|
void** (*GetBlock)(struct mem_ctl*,int);
|
||||||
|
//释放一个内存块
|
||||||
|
int (*FreeBlock)(struct mem_ctl*,void**);
|
||||||
|
}mem_ctl;
|
||||||
|
|
||||||
|
int init_memctl(mem_ctl *self);
|
||||||
|
int free_memctl(mem_ctl *self);
|
||||||
|
|
||||||
|
#endif
|
||||||
18
c/network/cJSON.c
Normal file → Executable file
18
c/network/cJSON.c
Normal file → Executable file
@ -160,25 +160,11 @@ typedef struct internal_hooks
|
|||||||
void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
|
void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
|
||||||
} internal_hooks;
|
} 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_malloc malloc
|
||||||
#define internal_free free
|
#define internal_free free
|
||||||
#define internal_realloc realloc
|
#define internal_realloc realloc
|
||||||
#endif
|
|
||||||
|
|
||||||
/* strlen of character literals resolved at compile time */
|
/* strlen of character literals resolved at compile time */
|
||||||
#define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
|
#define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
|
||||||
|
|||||||
0
c/network/cJSON.h
Normal file → Executable file
0
c/network/cJSON.h
Normal file → Executable file
0
c/network/erroprocess/erroprocess.c
Normal file → Executable file
0
c/network/erroprocess/erroprocess.c
Normal file → Executable file
0
c/network/erroprocess/erroprocess.h
Normal file → Executable file
0
c/network/erroprocess/erroprocess.h
Normal file → Executable file
121
c/network/http/http_rel.c
Normal file → Executable file
121
c/network/http/http_rel.c
Normal file → Executable file
@ -5,71 +5,18 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
#include "errno.h"
|
#include "errno.h"
|
||||||
#include "tools/log/log.h"
|
#include "tools/log/log.h"
|
||||||
#include "http_rel.h"
|
#include "http_rel.h"
|
||||||
#include <netinet/in.h>
|
|
||||||
|
|
||||||
int write_in_bk(char input,httpbuf* blk){
|
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){
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -81,49 +28,48 @@ char *recv_http_request(int cfd){
|
|||||||
*/
|
*/
|
||||||
int init_http_network(int port, log_manager *logger)
|
int init_http_network(int port, log_manager *logger)
|
||||||
{
|
{
|
||||||
logs *log;
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
|
int fd;
|
||||||
|
char log[MAX_LOG_LENGTH];
|
||||||
/* 1. 创建socket */
|
/* 1. 创建socket */
|
||||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
log = malloc(sizeof(logs));
|
snprintf(log, sizeof(log),
|
||||||
// cppcheck-suppress uninitdata
|
"socket() failed: %s", strerror(errno));
|
||||||
snprintf(log->log, sizeof(log->log),
|
logger->in_log(logger, log,"FATAL");
|
||||||
"[FATAL] socket() failed: %s", strerror(errno));
|
|
||||||
logger->in_log(log, logger);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 2. 设置SO_REUSEADDR,避免TIME_WAIT状态导致bind失败 */
|
/* 2. 设置SO_REUSEADDR,避免TIME_WAIT状态导致bind失败 */
|
||||||
int opt = 1;
|
int opt = 1;
|
||||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
|
||||||
log = malloc(sizeof(logs));
|
snprintf(log, sizeof(log),
|
||||||
snprintf(log->log, sizeof(log->log),
|
"setsockopt(SO_REUSEADDR) on fd=%d failed: %s",
|
||||||
"[ERROR] setsockopt(SO_REUSEADDR) on fd=%d failed: %s",
|
|
||||||
fd, strerror(errno));
|
fd, strerror(errno));
|
||||||
logger->in_log(log, logger);
|
logger->in_log(logger,log,"ERROR:");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 3. 设置为非阻塞模式(配合epoll使用) */
|
/* 3. 设置为非阻塞模式(配合epoll使用) */
|
||||||
int flags = fcntl(fd, F_GETFL, 0);
|
int flags = fcntl(fd, F_GETFL, 0);
|
||||||
if (flags == -1) {
|
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);
|
close(fd);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||||
log = malloc(sizeof(logs));
|
snprintf(log, sizeof(log),
|
||||||
snprintf(log->log, sizeof(log->log),
|
"fcntl(O_NONBLOCK) on fd=%d failed: %s", fd, strerror(errno));
|
||||||
"[ERROR] fcntl(O_NONBLOCK) on fd=%d failed: %s", fd, strerror(errno));
|
logger->in_log(logger,log,"ERROR:");
|
||||||
logger->in_log(log, logger);
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,30 +80,29 @@ int init_http_network(int port, log_manager *logger)
|
|||||||
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 监听所有网卡
|
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 监听所有网卡
|
||||||
|
|
||||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
|
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
|
||||||
log = malloc(sizeof(logs));
|
snprintf(log, sizeof(log),
|
||||||
snprintf(log->log, sizeof(log->log),
|
"bind(port %d) failed: %s (fd=%d)",
|
||||||
"[FATAL] bind(port %d) failed: %s (fd=%d)",
|
|
||||||
port, strerror(errno), fd);
|
port, strerror(errno), fd);
|
||||||
logger->in_log(log, logger);
|
logger->in_log(logger,log,"FATAL:");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 5. 开始监听 */
|
/* 5. 开始监听 */
|
||||||
if (listen(fd, 10) == -1) {
|
if (listen(fd, 10) == -1) {
|
||||||
log = malloc(sizeof(logs));
|
snprintf(log, sizeof(log),
|
||||||
snprintf(log->log, sizeof(log->log),
|
"listen(fd=%d, backlog=10) failed: %s",
|
||||||
"[FATAL] listen(fd=%d, backlog=10) failed: %s",
|
|
||||||
fd, strerror(errno));
|
fd, strerror(errno));
|
||||||
logger->in_log(log, logger);
|
logger->in_log(logger,log,"FATAL:");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 6. 成功日志 */
|
/* 6. 成功日志 */
|
||||||
log = malloc(sizeof(logs));
|
snprintf(log, sizeof(log),
|
||||||
snprintf(log->log, sizeof(log->log),
|
"Successfully listening on port %d (fd=%d)", port, fd);
|
||||||
"[HTTP] Successfully listening on port %d (fd=%d)", port, fd);
|
logger->in_log(logger, log,"HTTP:");
|
||||||
logger->in_log(log, logger);
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|||||||
3
c/network/http/http_rel.h
Normal file → Executable file
3
c/network/http/http_rel.h
Normal file → Executable file
@ -2,10 +2,11 @@
|
|||||||
#define HTTP_REL
|
#define HTTP_REL
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "memctl/memctl.h"
|
||||||
|
|
||||||
typedef struct httpbuf{
|
typedef struct httpbuf{
|
||||||
char *buf;
|
|
||||||
int size;
|
int size;
|
||||||
|
char buf[MEM_BLOCK_SIZE-5];
|
||||||
}httpbuf;//http分块结构体
|
}httpbuf;//http分块结构体
|
||||||
|
|
||||||
char *recv_http_request(int cfd);
|
char *recv_http_request(int cfd);
|
||||||
|
|||||||
19
c/network/network.c
Normal file → Executable file
19
c/network/network.c
Normal file → Executable file
@ -33,6 +33,8 @@ static void safe_strcpy(char *dst, size_t dst_size, const char *src)
|
|||||||
/* 主解析 */
|
/* 主解析 */
|
||||||
int rbt_parse_json(const char *json_text, rbt_msg *out)
|
int rbt_parse_json(const char *json_text, rbt_msg *out)
|
||||||
{
|
{
|
||||||
|
if(json_text == NULL)
|
||||||
|
return -1;
|
||||||
memset(out, 0, sizeof(*out)); // 统一清 0,gid 天然 '\0'
|
memset(out, 0, sizeof(*out)); // 统一清 0,gid 天然 '\0'
|
||||||
|
|
||||||
cJSON *root = cJSON_Parse(json_text);
|
cJSON *root = cJSON_Parse(json_text);
|
||||||
@ -101,14 +103,14 @@ int process_message(char *req, log_manager *logger,rbt_msg *swap) {
|
|||||||
const char *body = recv_http_request(fd);
|
const char *body = recv_http_request(fd);
|
||||||
|
|
||||||
if(rbt_parse_json(body,swap) == 0) {
|
if(rbt_parse_json(body,swap) == 0) {
|
||||||
logs *log = malloc(sizeof(logs));
|
char log[MAX_LOG_LENGTH];
|
||||||
// cppcheck-suppress uninitdata
|
// 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);
|
swap->nickname,swap->raw_message);
|
||||||
make_swap(swap);
|
make_swap(swap);
|
||||||
logger->in_log(log, logger);
|
logger->in_log(logger,log,"PROCESSER:");
|
||||||
}
|
}
|
||||||
|
//通知前端已收到消息
|
||||||
const char *response =
|
const char *response =
|
||||||
"HTTP/1.1 200 OK\r\n"
|
"HTTP/1.1 200 OK\r\n"
|
||||||
"Content-Type: text/plain\r\n"
|
"Content-Type: text/plain\r\n"
|
||||||
@ -163,11 +165,11 @@ void *pth_module(void *args_p)
|
|||||||
NULL};
|
NULL};
|
||||||
execv("Run_pluhginmanager",args);
|
execv("Run_pluhginmanager",args);
|
||||||
}
|
}
|
||||||
logs *pth_log = (logs*)malloc(sizeof(logs));
|
char pth_log[40];
|
||||||
// cppcheck-suppress uninitdata
|
// 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);
|
rbt_msg *swap = (rbt_msg*)mmap(NULL, sizeof(rbt_msg), PROT_READ|PROT_WRITE, MAP_SHARED,swapfd, 0);
|
||||||
//拉起python插件管理器
|
//拉起python插件管理器
|
||||||
for(;;){
|
for(;;){
|
||||||
@ -183,9 +185,6 @@ void *pth_module(void *args_p)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
pth_log = (logs*)malloc(sizeof(logs));
|
|
||||||
sprintf(pth_log->log,"processd message");
|
|
||||||
logger->in_log(pth_log,logger);
|
|
||||||
process_message(req,logger,swap);
|
process_message(req,logger,swap);
|
||||||
atomic_fetch_add(&pmd->status, 1);
|
atomic_fetch_add(&pmd->status, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
0
c/network/network.h
Normal file → Executable file
0
c/network/network.h
Normal file → Executable file
17
c/network/protocal.h
Normal file
17
c/network/protocal.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef PROTOCAL
|
||||||
|
#define PROTOCAL
|
||||||
|
|
||||||
|
typedef struct network_pakage
|
||||||
|
{
|
||||||
|
char *data;
|
||||||
|
int buf_block;
|
||||||
|
}network_package;
|
||||||
|
|
||||||
|
typedef struct net_protocal
|
||||||
|
{
|
||||||
|
void *protocal;
|
||||||
|
int (*init)(void *self);
|
||||||
|
int (*rev_message)(void *self,network_package *data);
|
||||||
|
}net_protocal;
|
||||||
|
|
||||||
|
#endif
|
||||||
2
c/network/swap.c
Normal file → Executable file
2
c/network/swap.c
Normal file → Executable file
@ -39,7 +39,7 @@ int create_swap(const char *name)
|
|||||||
memcpy(init_msg->nickname,buf,64);
|
memcpy(init_msg->nickname,buf,64);
|
||||||
sem_init(&init_msg->status,1,1);
|
sem_init(&init_msg->status,1,1);
|
||||||
init_msg->raw_message[0] = '\0';
|
init_msg->raw_message[0] = '\0';
|
||||||
init_msg->state = FREE;
|
init_msg->state = MEM_FREE;
|
||||||
init_msg->uid[0] = '\0';
|
init_msg->uid[0] = '\0';
|
||||||
munmap((void*)init_msg,sizeof(rbt_msg));
|
munmap((void*)init_msg,sizeof(rbt_msg));
|
||||||
return fd;
|
return fd;
|
||||||
|
|||||||
2
c/network/swap.h
Normal file → Executable file
2
c/network/swap.h
Normal file → Executable file
@ -4,7 +4,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#define QUITPLG 0
|
#define QUITPLG 0
|
||||||
#define NEWMSG 1
|
#define NEWMSG 1
|
||||||
#define FREE 2
|
#define SW_FREE 2
|
||||||
|
|
||||||
int make_swap(rbt_msg *swap);
|
int make_swap(rbt_msg *swap);
|
||||||
int create_swap(const char *name);
|
int create_swap(const char *name);
|
||||||
|
|||||||
0
c/run_pluginmanager/run_pluginmanager.c
Normal file → Executable file
0
c/run_pluginmanager/run_pluginmanager.c
Normal file → Executable file
0
c/run_pluginmanager/run_pluginmanager.h
Normal file → Executable file
0
c/run_pluginmanager/run_pluginmanager.h
Normal file → Executable file
5
c/tem/ctl.c
Normal file → Executable file
5
c/tem/ctl.c
Normal file → Executable file
@ -252,9 +252,8 @@ int teml(Ctl *self,int fifo[2])
|
|||||||
perror("sys error");
|
perror("sys error");
|
||||||
//将用户输入入队
|
//将用户输入入队
|
||||||
infifo(self,input);
|
infifo(self,input);
|
||||||
logs *log = (logs*)malloc(sizeof(logs));
|
|
||||||
memcpy(log->log,input,sizeof(input));
|
self->logmanager->in_log(self->logmanager,input,"USER:");
|
||||||
self->logmanager->in_log(log,self->logmanager);
|
|
||||||
memcpy(command->command,input,sizeof(input));
|
memcpy(command->command,input,sizeof(input));
|
||||||
interpret(SIG_MOD,command,cmd_dir);
|
interpret(SIG_MOD,command,cmd_dir);
|
||||||
const char fexp[256] = {'\0'};
|
const char fexp[256] = {'\0'};
|
||||||
|
|||||||
0
c/tem/ctl.h
Normal file → Executable file
0
c/tem/ctl.h
Normal file → Executable file
208
c/tools/log/log.c
Normal file → Executable file
208
c/tools/log/log.c
Normal file → Executable file
@ -9,48 +9,75 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int in_log(logs *log,log_manager *self)
|
|
||||||
|
logs* getbody(void **log_p)
|
||||||
{
|
{
|
||||||
if(log == NULL||self == NULL)
|
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;
|
return -1;
|
||||||
log->log[MAX_LOG_LENGTH-1] = '\0';
|
if(*length+strlen(org)<maxlength-1)
|
||||||
sem_wait(&self->log_sem);//加锁
|
{
|
||||||
if(self->log == NULL){
|
strcpy(&writein[*length],org);
|
||||||
self->log = log;
|
*length +=strlen(org);//栈内存充足
|
||||||
self->rear = log;
|
|
||||||
self->count++;
|
|
||||||
sem_post(&self->log_sem);
|
|
||||||
return self->count;
|
|
||||||
}
|
}
|
||||||
if(self->count == 1)
|
|
||||||
self->log->next = log;
|
else
|
||||||
self->count++;
|
{
|
||||||
log->next = NULL;
|
int n = *length + strlen(org) - maxlength+1;
|
||||||
self->rear->next = log;
|
strncpy(&writein[*length],org,strlen(org)-n);//栈内存不足
|
||||||
self->rear = log;
|
writein[maxlength-1] = '\0';
|
||||||
sem_post(&self->log_sem);
|
int fd = open(logname,O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||||
return self->count;
|
if(fd != -1)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
logs *out_log(log_manager *self)
|
int in_log(log_manager *self,const char *logbody,const char *info)
|
||||||
{
|
{
|
||||||
if(self == NULL)
|
if(self == NULL)
|
||||||
return NULL;
|
return -1;
|
||||||
sem_wait(&self->log_sem);
|
void **log_p = self->mempool->GetBlock(self->mempool,LOGMOD);
|
||||||
logs *buf = self->log;
|
if(log_p == NULL)
|
||||||
if(buf==NULL)
|
|
||||||
{
|
{
|
||||||
sem_post(&self->log_sem);
|
perror("Mem_runout");
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
if(self->log->next ==NULL)
|
logs *log = getbody(log_p);
|
||||||
self->log = self->rear = NULL;
|
snprintf(log->info,INFO_LENGTH,"%s",info);
|
||||||
else
|
snprintf(log->log,MAX_LOG_LENGTH,"%s",logbody);
|
||||||
self->log = self->log->next;
|
log->log[MAX_LOG_LENGTH-1] = '\0';
|
||||||
self->count--;
|
log->next = NULL;
|
||||||
|
sem_wait(&self->log_sem);//加锁
|
||||||
|
if(self->log == NULL){
|
||||||
|
self->log = log_p;
|
||||||
|
self->rear = getbody(log_p);
|
||||||
|
atomic_fetch_add(&self->count,1);
|
||||||
sem_post(&self->log_sem);
|
sem_post(&self->log_sem);
|
||||||
buf->next =NULL;
|
return self->count;
|
||||||
return buf;
|
}
|
||||||
|
if(self->count == 1){
|
||||||
|
logs *p = getbody(self->log);
|
||||||
|
p->next = log_p;
|
||||||
|
}
|
||||||
|
self->count++;
|
||||||
|
log->next = NULL;
|
||||||
|
self->rear->next = log_p;
|
||||||
|
self->rear = log;
|
||||||
|
sem_post(&self->log_sem);
|
||||||
|
return self->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sleep_with_signal(log_manager *self)
|
int sleep_with_signal(log_manager *self)
|
||||||
@ -62,7 +89,7 @@ int sleep_with_signal(log_manager *self)
|
|||||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
|
||||||
return -1; /* 罕见失败 */
|
return -1; /* 罕见失败 */
|
||||||
|
|
||||||
ts.tv_sec += 1000;
|
ts.tv_sec += LOG_SLEEP_LENGTH;
|
||||||
/* 纳秒部分无需处理,1000 s 整不会溢出 */
|
/* 纳秒部分无需处理,1000 s 整不会溢出 */
|
||||||
|
|
||||||
pthread_mutex_lock(&self->mtx); /* 进入临界区 */
|
pthread_mutex_lock(&self->mtx); /* 进入临界区 */
|
||||||
@ -91,94 +118,55 @@ int cleanup(log_manager *self)
|
|||||||
if(self->log ==NULL)
|
if(self->log ==NULL)
|
||||||
return 1;
|
return 1;
|
||||||
logs *tobeclean,*loc;
|
logs *tobeclean,*loc;
|
||||||
|
void **tobeclean_p;
|
||||||
sem_wait(&self->log_sem);//获取信号量
|
sem_wait(&self->log_sem);//获取信号量
|
||||||
loc = self->log;
|
void **loc_p = self->log;
|
||||||
|
|
||||||
self->log = NULL;
|
self->log = NULL;
|
||||||
self->count = 0;//摘取log链
|
atomic_store(&self->count,0);//摘取log链
|
||||||
sem_post(&self->log_sem);
|
sem_post(&self->log_sem);
|
||||||
//释放信号量
|
//释放信号量
|
||||||
char *logbuf;
|
loc = getbody(loc_p);
|
||||||
void *logbufbk;
|
|
||||||
char failback[MAX_LOG_LENGTH];
|
char logbuf[MAX_LOG_LENGTH];
|
||||||
logbuf = (char*)malloc(1);
|
|
||||||
logbuf[0] = '\0';
|
size_t buf_length = 0;
|
||||||
size_t buf_length,buf_lengthbk = 0;
|
|
||||||
int fd;
|
int fd;
|
||||||
while(loc->next !=NULL)
|
while(loc->next !=NULL)
|
||||||
{
|
{
|
||||||
tobeclean = loc;
|
tobeclean_p = loc_p;
|
||||||
loc = loc->next;
|
tobeclean = getbody(tobeclean_p);
|
||||||
if(logbuf != failback){
|
loc_p = loc->next;
|
||||||
logbufbk = logbuf;
|
loc = getbody(loc_p);
|
||||||
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;//降级策略,堆空间不足时,使用预分配栈空间,保留最后一条日志
|
|
||||||
}
|
|
||||||
if(logbuf != failback){
|
|
||||||
buf_lengthbk = buf_length;
|
|
||||||
buf_length = strlen(logbuf)+strlen(tobeclean->log);
|
|
||||||
strcpy(logbuf+buf_lengthbk,tobeclean->log);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buf_length = MAX_LOG_LENGTH;
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
|
|
||||||
|
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");//非业务逻辑只警告
|
||||||
|
|
||||||
|
self->mempool->FreeBlock(self->mempool,tobeclean_p);
|
||||||
}
|
}
|
||||||
else{
|
write_into_block(logbuf,loc->info,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||||
buf_lengthbk = buf_length;
|
write_into_block(logbuf,":",&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||||
buf_length = strlen(logbuf)+strlen(loc->log);
|
write_into_block(logbuf,loc->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||||
strcpy(logbuf+buf_lengthbk,loc->log);
|
|
||||||
}
|
self->mempool->FreeBlock(self,loc_p);
|
||||||
}
|
|
||||||
else{
|
|
||||||
strcpy(logbuf,loc->log);
|
|
||||||
buf_length = strlen(logbuf);
|
|
||||||
}
|
|
||||||
if(logbuf == failback&&buf_length>MAX_LOG_LENGTH)
|
|
||||||
logbuf[MAX_LOG_LENGTH-1] = '\0';
|
|
||||||
else{
|
|
||||||
logbuf[buf_length] = '\0';
|
|
||||||
}
|
|
||||||
free(loc);
|
|
||||||
|
|
||||||
fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||||
if(fd == -1){
|
if(fd == -1){
|
||||||
perror("file:");
|
perror("log:");
|
||||||
if(logbuf != failback)
|
|
||||||
free(logbuf);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
int error_buf = write(fd,logbuf,strlen(logbuf));
|
int error_buf = write(fd,logbuf,strlen(logbuf));
|
||||||
if(error_buf==-1){
|
if(error_buf==-1){
|
||||||
close(fd);
|
|
||||||
if(logbuf != failback)
|
|
||||||
free(logbuf);
|
|
||||||
return -1;
|
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);
|
write(fd,"unknown error case log write cut down\n",38);
|
||||||
if(logbuf != failback)
|
}
|
||||||
free(logbuf);
|
|
||||||
close(fd);
|
close(fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -193,13 +181,9 @@ void log_manager_stop(log_manager *self)
|
|||||||
self->stop = 1;
|
self->stop = 1;
|
||||||
/* 置退出标志 */
|
/* 置退出标志 */
|
||||||
printf("SYS:stopping loger\n");
|
printf("SYS:stopping loger\n");
|
||||||
logs *log = malloc(sizeof(logs));
|
self->in_log(self,"stopping loger\n","SYS:");
|
||||||
strcpy(log->log,"SYS:stopping loger\n");
|
|
||||||
self->in_log(log,self);
|
|
||||||
log = malloc(sizeof(logs));
|
|
||||||
strcpy(log->log,"SYS:done\n");
|
|
||||||
printf("SYS:done\n");
|
printf("SYS:done\n");
|
||||||
self->in_log(log,self);
|
self->in_log(self,"done","SYS:");
|
||||||
pthread_mutex_unlock(&self->mtx);
|
pthread_mutex_unlock(&self->mtx);
|
||||||
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
|
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
|
||||||
}
|
}
|
||||||
@ -224,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)
|
if(self == NULL)
|
||||||
{
|
{
|
||||||
@ -254,11 +238,11 @@ int init_loger(log_manager *self)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
self->in_log = in_log;
|
self->in_log = in_log;
|
||||||
self->out_log = out_log;
|
|
||||||
self->clear_log = clear_log;
|
self->clear_log = clear_log;
|
||||||
self->log = NULL;
|
self->log = NULL;
|
||||||
self->stop = 0;
|
self->stop = 0;
|
||||||
self->cleanup = cleanup;
|
self->cleanup = cleanup;
|
||||||
self->count = 0;
|
atomic_init(&self->count,0);
|
||||||
|
self->mempool = mempool;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
14
c/tools/log/log.h
Normal file → Executable file
14
c/tools/log/log.h
Normal file → Executable file
@ -2,6 +2,7 @@
|
|||||||
#define LOG
|
#define LOG
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "memctl/memctl.h"
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
@ -9,26 +10,27 @@
|
|||||||
typedef struct logs
|
typedef struct logs
|
||||||
{
|
{
|
||||||
char log[MAX_LOG_LENGTH];
|
char log[MAX_LOG_LENGTH];
|
||||||
struct logs *next;
|
void **next;
|
||||||
|
char info[INFO_LENGTH];
|
||||||
}logs;
|
}logs;
|
||||||
|
|
||||||
typedef struct log_manager
|
typedef struct log_manager
|
||||||
{
|
{
|
||||||
pthread_t pid;
|
pthread_t pid;
|
||||||
int (*in_log)(logs *,struct log_manager*);
|
mem_ctl *mempool;
|
||||||
logs* (*out_log)(struct log_manager*);
|
int (*in_log)(struct log_manager*,const char *,const char *);
|
||||||
void *(*clear_log)(void*);
|
void *(*clear_log)(void*);
|
||||||
int (*cleanup)(struct log_manager*);
|
int (*cleanup)(struct log_manager*);
|
||||||
sem_t log_sem;
|
sem_t log_sem;
|
||||||
logs *log;
|
void **log;
|
||||||
logs *rear;
|
logs *rear;
|
||||||
int count;
|
atomic_int count;
|
||||||
pthread_mutex_t mtx;
|
pthread_mutex_t mtx;
|
||||||
pthread_cond_t cond;
|
pthread_cond_t cond;
|
||||||
int stop;
|
int stop;
|
||||||
}log_manager;
|
}log_manager;
|
||||||
|
|
||||||
void log_manager_stop(log_manager *self);
|
void log_manager_stop(log_manager *self);
|
||||||
int init_loger(log_manager *self);
|
int init_loger(log_manager *self,mem_ctl *mempool);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
0
c/tools/pkgmanager/pkginstall.c
Normal file → Executable file
0
c/tools/pkgmanager/pkginstall.c
Normal file → Executable file
0
c/tools/pkgmanager/pkginstall.h
Normal file → Executable file
0
c/tools/pkgmanager/pkginstall.h
Normal file → Executable file
0
c/tools/pkgmanager/update_pkg.c
Normal file → Executable file
0
c/tools/pkgmanager/update_pkg.c
Normal file → Executable file
0
c/tools/pkgmanager/update_pkg.h
Normal file → Executable file
0
c/tools/pkgmanager/update_pkg.h
Normal file → Executable file
13
c/tools/quit/quit.c
Normal file → Executable file
13
c/tools/quit/quit.c
Normal file → Executable file
@ -11,6 +11,7 @@
|
|||||||
#include "tem/ctl.h"
|
#include "tem/ctl.h"
|
||||||
#include "tools/toml/toml.h"
|
#include "tools/toml/toml.h"
|
||||||
|
|
||||||
|
|
||||||
int quit_server(netm *self)
|
int quit_server(netm *self)
|
||||||
{
|
{
|
||||||
if(self ==NULL)
|
if(self ==NULL)
|
||||||
@ -44,15 +45,18 @@ int quit_server(netm *self)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int quit_mempool(mem_ctl *mem_ctler)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void quit_all(int status,void *self_p)
|
void quit_all(int status,void *self_p)
|
||||||
{
|
{
|
||||||
alres *resouce =(alres*)self_p;
|
alres *resouce =(alres*)self_p;
|
||||||
//转换参数
|
//转换参数
|
||||||
|
|
||||||
resouce->network->shutdown_pool(resouce->network);
|
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)
|
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->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);
|
free(resouce->network);
|
||||||
//释放网络资源
|
//释放网络资源
|
||||||
if(resouce->tem->command !=NULL){
|
if(resouce->tem->command !=NULL){
|
||||||
@ -97,4 +101,5 @@ void quit_all(int status,void *self_p)
|
|||||||
|
|
||||||
free(resouce->loger);
|
free(resouce->loger);
|
||||||
//清理日志
|
//清理日志
|
||||||
|
free(resouce);
|
||||||
}
|
}
|
||||||
|
|||||||
3
c/tools/quit/quit.h
Normal file → Executable file
3
c/tools/quit/quit.h
Normal file → Executable file
@ -4,11 +4,14 @@
|
|||||||
#include "network/network.h"
|
#include "network/network.h"
|
||||||
#include "tem/ctl.h"
|
#include "tem/ctl.h"
|
||||||
#include "tools/log/log.h"
|
#include "tools/log/log.h"
|
||||||
|
#include "memctl/memctl.h"
|
||||||
|
|
||||||
typedef struct all_resources
|
typedef struct all_resources
|
||||||
{
|
{
|
||||||
Ctl *tem;
|
Ctl *tem;
|
||||||
netm *network;
|
netm *network;
|
||||||
log_manager *loger;
|
log_manager *loger;
|
||||||
|
mem_ctl *memctler;
|
||||||
|
|
||||||
}alres;
|
}alres;
|
||||||
|
|
||||||
|
|||||||
0
c/tools/toml/toml.c
Normal file → Executable file
0
c/tools/toml/toml.c
Normal file → Executable file
0
c/tools/toml/toml.h
Normal file → Executable file
0
c/tools/toml/toml.h
Normal file → Executable file
0
config/config.toml
Normal file → Executable file
0
config/config.toml
Normal file → Executable file
0
src/file_store_api.py
Normal file → Executable file
0
src/file_store_api.py
Normal file → Executable file
0
src/modules/__init__.py
Normal file → Executable file
0
src/modules/__init__.py
Normal file → Executable file
0
src/modules/plugin_modules.py
Normal file → Executable file
0
src/modules/plugin_modules.py
Normal file → Executable file
0
src/modules/user_modules.py
Normal file → Executable file
0
src/modules/user_modules.py
Normal file → Executable file
0
src/plugin_manager.py
Normal file → Executable file
0
src/plugin_manager.py
Normal file → Executable file
Reference in New Issue
Block a user