Compare commits
1 Commits
main
...
264b14e1c1
| Author | SHA1 | Date | |
|---|---|---|---|
| 264b14e1c1 |
2
.gitignore
vendored
Executable file → Normal file
2
.gitignore
vendored
Executable file → Normal file
@ -6,7 +6,7 @@ __pycache__/
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
.vscode/
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
|
||||
3
c/CMakeLists.txt
Executable file → Normal file
3
c/CMakeLists.txt
Executable file → Normal file
@ -17,8 +17,7 @@ add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstal
|
||||
add_library(Log SHARED tools/log/log.c)
|
||||
add_library(Toml SHARED tools/toml/toml.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 Memctl)
|
||||
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit)
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR})
|
||||
24
c/config.h
Executable file → Normal file
24
c/config.h
Executable file → Normal file
@ -1,37 +1,17 @@
|
||||
#ifndef SEVERCONFG
|
||||
#define SEVERCONFG
|
||||
|
||||
/*------日志管理---------*/
|
||||
#define MAX_LOG 50
|
||||
#define MAX_LOG_LENGTH 4080
|
||||
#define INFO_LENGTH 8
|
||||
#define LOG_SLEEP_LENGTH 1
|
||||
/*------日志管理---------*/
|
||||
#define MAX_LOG 256
|
||||
|
||||
/*-------终端管理-------*/
|
||||
#define TEM_MAX_BUF 256
|
||||
#define TEM_HISTORY_BUF 210
|
||||
#define TEM_PROMPT "chatbot$$ "
|
||||
/*----终端管理--------*/
|
||||
|
||||
/*-------网路池管理-----*/
|
||||
#define NET_MAX_POOL 1
|
||||
#define NET_MAX_POOL 10
|
||||
#define NET_MAX_MESSAGE_BUF 1024
|
||||
#define HTTP_BLOCK_SIZE 512
|
||||
#define MAX_HTTP_LENGTH 20
|
||||
/*-------网路池管理-----*/
|
||||
|
||||
/*------解释器管理-------*/
|
||||
#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
|
||||
34
c/interpreter/interpreter.c
Executable file → Normal file
34
c/interpreter/interpreter.c
Executable file → Normal file
@ -9,22 +9,24 @@
|
||||
#include "interpreter.h"
|
||||
#include "tools/pkgmanager/pkginstall.h"
|
||||
|
||||
int inter_in_log(const char *log,const char *info,log_manager *manager)
|
||||
int inter_in_log(const char *log,log_manager *manager)
|
||||
{
|
||||
if(strlen(log)>1024)
|
||||
return -1;
|
||||
manager->in_log(manager,log,info);
|
||||
logs* logss = (logs*)malloc(sizeof(logs));
|
||||
memcpy(logss->log,log,strlen(log));
|
||||
manager->in_log(logss,manager);
|
||||
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("prepare env\n","SYS",log_manager);
|
||||
inter_in_log("SYS:prepare env\n",log_manager);
|
||||
printf("SYS:env ready\n");
|
||||
inter_in_log("env ready\n","SYS",log_manager);
|
||||
inter_in_log("SYS:env ready\n",log_manager);
|
||||
printf("SYS:loading cmd_dic\n");
|
||||
inter_in_log("loading cmd_dic\n","SYS",log_manager);
|
||||
inter_in_log("SYS:loading cmd_dic\n",log_manager);
|
||||
sprintf(cmd_dic[0].name, "pkginstall");
|
||||
cmd_dic[0].cmd = INSTALL;
|
||||
|
||||
@ -35,7 +37,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("cmd_dir load complite\n","SYS",log_manager);
|
||||
inter_in_log("SYS:cmd_dir load complite\n",log_manager);
|
||||
|
||||
for(int i =0;i<10;i++)
|
||||
{
|
||||
@ -43,7 +45,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("Creating ctl fifo\n","SYS",log_manager);
|
||||
inter_in_log("SYS:Creating ctl fifo\n",log_manager);
|
||||
memcpy(self->fifofd,fifo,2*sizeof(int));
|
||||
self->log_manager = log_manager;
|
||||
}
|
||||
@ -69,7 +71,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("fail to get mem\n","ERROR",self->log_manager);
|
||||
inter_in_log("ERROR:fail to get mem\n",self->log_manager);
|
||||
return -1;
|
||||
}
|
||||
arg = arg->next;
|
||||
@ -137,38 +139,38 @@ int match_cmd(const Cmd* cmd_dic,char *cmd_buf)
|
||||
}
|
||||
|
||||
|
||||
int exec(const int command,ctx *all_ctx)
|
||||
int exce(const int command,ctx *all_ctx)
|
||||
{
|
||||
|
||||
switch (command)
|
||||
{
|
||||
case BAD_INPUT:
|
||||
printf("SYS:bad input,try again\n");
|
||||
inter_in_log("bad input,try again\n","SYS",all_ctx->log_manager);
|
||||
inter_in_log("SYS:bad input,try again\n",all_ctx->log_manager);
|
||||
return BAD_INPUT;
|
||||
|
||||
case INSTALL:
|
||||
if(all_ctx->arg == NULL){
|
||||
printf("SYS:Missing args\n");
|
||||
inter_in_log("Missng args\n","SYS",all_ctx->log_manager);
|
||||
inter_in_log("SYS:Missng args\n",all_ctx->log_manager);
|
||||
return 1;
|
||||
}
|
||||
printf("SYS:init pkgmanager\n");
|
||||
inter_in_log("init pkgmanager\n","SYS",all_ctx->log_manager);
|
||||
inter_in_log("SYS:init pkgmanager\n",all_ctx->log_manager);
|
||||
pkger *manager = init_pkginstaller();
|
||||
printf("SYS:installing\n");
|
||||
inter_in_log("installing\n","SYS",all_ctx->log_manager);
|
||||
inter_in_log("SYS:installing\n",all_ctx->log_manager);
|
||||
manager->packup(manager);
|
||||
return 1;
|
||||
|
||||
case RUN:
|
||||
printf("SYS:runing\n");
|
||||
inter_in_log("running\n","SYS",all_ctx->log_manager);
|
||||
inter_in_log("SYS:running\n",all_ctx->log_manager);
|
||||
return 1;
|
||||
|
||||
case QUIT:
|
||||
printf("SYS:shuting down\n");
|
||||
inter_in_log("shuting down\n","SYS",all_ctx->log_manager);
|
||||
inter_in_log("SYS:shuting down\n",all_ctx->log_manager);
|
||||
all_ctx->statue = -1;
|
||||
write(all_ctx->fifofd[1],"q",1);
|
||||
return 1;
|
||||
@ -200,7 +202,7 @@ int interpret(int mod, ctx *all_ctx,Cmd *cmd_dic)
|
||||
if(cmd_buf[len-1] == '\n')
|
||||
cmd_buf[len-1] = '\0';
|
||||
//执行命令
|
||||
exec(match_cmd(cmd_dic, cmd_buf),all_ctx);
|
||||
exce(match_cmd(cmd_dic, cmd_buf),all_ctx);
|
||||
|
||||
//释放所有堆内存
|
||||
free(cmd_buf);
|
||||
|
||||
0
c/interpreter/interpreter.h
Executable file → Normal file
0
c/interpreter/interpreter.h
Executable file → Normal file
22
c/main.c
Executable file → Normal file
22
c/main.c
Executable file → Normal file
@ -3,8 +3,6 @@
|
||||
#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>
|
||||
@ -35,12 +33,9 @@ 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,mem_ctler);
|
||||
pthread_create(&logsmanager->pid,NULL,logsmanager->clear_log,logsmanager);
|
||||
init_loger(logsmanager);
|
||||
|
||||
Ctl *teml = init_tem(logsmanager);
|
||||
teml->config = server;
|
||||
//初始化终端对象
|
||||
@ -50,27 +45,20 @@ int main()
|
||||
netm *networkmanager = (netm*)malloc(sizeof(netm));
|
||||
init_networkmanager(networkmanager,fifo,logsmanager,port);
|
||||
//初始化网络管理器对象
|
||||
|
||||
pthread_create(&networkmanager->pid,NULL,networkmanager->run_network,(void*)networkmanager);
|
||||
pthread_t network_id;
|
||||
pthread_create(&network_id,NULL,networkmanager->run_network,(void*)networkmanager);
|
||||
//启动网络监听与线程池,并加载插件
|
||||
alres *resource = (alres*)malloc(sizeof(alres));
|
||||
resource->loger = logsmanager;
|
||||
resource->network = networkmanager;
|
||||
resource->tem = teml;
|
||||
resource->memctler = mem_ctler;
|
||||
on_exit(quit_all,resource);
|
||||
//注册清理函数
|
||||
teml->run(teml,fifo);
|
||||
|
||||
//启动终端
|
||||
|
||||
pthread_join(network_id,NULL);
|
||||
//等待网络管理器进程结束
|
||||
pthread_join(networkmanager->pid,NULL);
|
||||
networkmanager->pid = -1;
|
||||
close(fifo[1]);
|
||||
log_manager_stop(logsmanager);
|
||||
pthread_join(logsmanager->pid,NULL);
|
||||
logsmanager->pid = -1;
|
||||
return 1;
|
||||
|
||||
}
|
||||
@ -1,159 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
#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
Executable file → Normal file
18
c/network/cJSON.c
Executable file → Normal file
@ -160,11 +160,25 @@ 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(""))
|
||||
|
||||
0
c/network/cJSON.h
Executable file → Normal file
0
c/network/cJSON.h
Executable file → Normal file
0
c/network/erroprocess/erroprocess.c
Executable file → Normal file
0
c/network/erroprocess/erroprocess.c
Executable file → Normal file
0
c/network/erroprocess/erroprocess.h
Executable file → Normal file
0
c/network/erroprocess/erroprocess.h
Executable file → Normal file
123
c/network/http/http_rel.c
Executable file → Normal file
123
c/network/http/http_rel.c
Executable file → Normal file
@ -5,18 +5,71 @@
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "errno.h"
|
||||
#include "tools/log/log.h"
|
||||
#include "http_rel.h"
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -28,48 +81,49 @@ 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) {
|
||||
snprintf(log, sizeof(log),
|
||||
"socket() failed: %s", strerror(errno));
|
||||
logger->in_log(logger, log,"FATAL");
|
||||
|
||||
log = malloc(sizeof(logs));
|
||||
// cppcheck-suppress uninitdata
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] socket() failed: %s", strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 2. 设置SO_REUSEADDR,避免TIME_WAIT状态导致bind失败 */
|
||||
int opt = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
|
||||
snprintf(log, sizeof(log),
|
||||
"setsockopt(SO_REUSEADDR) on fd=%d failed: %s",
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[ERROR] setsockopt(SO_REUSEADDR) on fd=%d failed: %s",
|
||||
fd, strerror(errno));
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
logger->in_log(log, logger);
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 3. 设置为非阻塞模式(配合epoll使用) */
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags == -1) {
|
||||
|
||||
snprintf(log, sizeof(log),
|
||||
"fcntl(F_GETFL) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
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);
|
||||
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||
snprintf(log, sizeof(log),
|
||||
"fcntl(O_NONBLOCK) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
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);
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -80,29 +134,30 @@ 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) {
|
||||
snprintf(log, sizeof(log),
|
||||
"bind(port %d) failed: %s (fd=%d)",
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] bind(port %d) failed: %s (fd=%d)",
|
||||
port, strerror(errno), fd);
|
||||
logger->in_log(logger,log,"FATAL:");
|
||||
logger->in_log(log, logger);
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 5. 开始监听 */
|
||||
if (listen(fd, 10) == -1) {
|
||||
snprintf(log, sizeof(log),
|
||||
"listen(fd=%d, backlog=10) failed: %s",
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] listen(fd=%d, backlog=10) failed: %s",
|
||||
fd, strerror(errno));
|
||||
logger->in_log(logger,log,"FATAL:");
|
||||
logger->in_log(log, logger);
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 6. 成功日志 */
|
||||
snprintf(log, sizeof(log),
|
||||
"Successfully listening on port %d (fd=%d)", port, fd);
|
||||
logger->in_log(logger, log,"HTTP:");
|
||||
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);
|
||||
return fd;
|
||||
}
|
||||
|
||||
3
c/network/http/http_rel.h
Executable file → Normal file
3
c/network/http/http_rel.h
Executable file → Normal file
@ -2,11 +2,10 @@
|
||||
#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);
|
||||
|
||||
74
c/network/network.c
Executable file → Normal file
74
c/network/network.c
Executable file → Normal file
@ -33,8 +33,6 @@ static void safe_strcpy(char *dst, size_t dst_size, const char *src)
|
||||
/* 主解析 */
|
||||
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'
|
||||
|
||||
cJSON *root = cJSON_Parse(json_text);
|
||||
@ -103,14 +101,14 @@ 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) {
|
||||
char log[MAX_LOG_LENGTH];
|
||||
logs *log = malloc(sizeof(logs));
|
||||
// cppcheck-suppress uninitdata
|
||||
snprintf(log, sizeof(log), "%s message %s processed ok\n",
|
||||
snprintf(log->log, sizeof(log->log), "%s message %s processed ok\n",
|
||||
swap->nickname,swap->raw_message);
|
||||
make_swap(swap);
|
||||
logger->in_log(logger,log,"PROCESSER:");
|
||||
logger->in_log(log, logger);
|
||||
}
|
||||
//通知前端已收到消息
|
||||
|
||||
const char *response =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
@ -165,11 +163,11 @@ void *pth_module(void *args_p)
|
||||
NULL};
|
||||
execv("Run_pluhginmanager",args);
|
||||
}
|
||||
char pth_log[40];
|
||||
logs *pth_log = (logs*)malloc(sizeof(logs));
|
||||
// cppcheck-suppress uninitdata
|
||||
sprintf(pth_log,"launched python plugines,pid:%ld\n",pthread_self());
|
||||
sprintf(pth_log->log,"PID:%lu launched python plugines\n",pthread_self());
|
||||
|
||||
logger->in_log(logger,pth_log,"PROCESSER:");
|
||||
logger->in_log(pth_log,logger);
|
||||
rbt_msg *swap = (rbt_msg*)mmap(NULL, sizeof(rbt_msg), PROT_READ|PROT_WRITE, MAP_SHARED,swapfd, 0);
|
||||
//拉起python插件管理器
|
||||
for(;;){
|
||||
@ -185,6 +183,9 @@ 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);
|
||||
}
|
||||
@ -216,7 +217,6 @@ int shutdown_pool(netm *self)
|
||||
self->pool[i].status = -1;
|
||||
close(self->pool[i].fifo_fd[1]);
|
||||
}
|
||||
self->statue = ALL_STOP;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -239,11 +239,11 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, self->http_fd, &ev);
|
||||
struct epoll_event events[10];
|
||||
self->epoll_fd = epfd;
|
||||
self->statue = SERVER_ON;
|
||||
for(;;)
|
||||
{
|
||||
/*工作循环-----------------------------*/
|
||||
int nf = epoll_wait(epfd,events,10,-1);
|
||||
printf("%d\n",nf);
|
||||
if (nf == -1) {
|
||||
perror("epoll_wait");
|
||||
break;
|
||||
@ -259,43 +259,21 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
sprintf(iss_buf,"s/%d/e",nt_fd);
|
||||
self->iss_work(self,iss_buf);
|
||||
}
|
||||
if(events[i].data.fd == fifo_fd) {
|
||||
char buffer[256];
|
||||
ssize_t bytes_read;
|
||||
|
||||
// 一次性读取所有可用数据
|
||||
bytes_read = read(fifo_fd, buffer, sizeof(buffer));
|
||||
|
||||
if (bytes_read > 0) {
|
||||
printf("DEBUG: Read %zd bytes from pipe: ", bytes_read);
|
||||
for (int j = 0; j < bytes_read; j++) {
|
||||
printf("%c ", buffer[j]);
|
||||
if(events[i].data.fd == fifo_fd)
|
||||
{
|
||||
char command;
|
||||
while(read(fifo_fd,&command,1)==1)
|
||||
{
|
||||
switch(command){
|
||||
case 'q':
|
||||
//退出逻辑
|
||||
quit_server(self);
|
||||
return 1;
|
||||
break;
|
||||
case 'u':
|
||||
//插件更新逻辑
|
||||
break;
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// 处理每个命令(按接收顺序)
|
||||
for (int j = 0; j < bytes_read; j++) {
|
||||
printf("Processing command[%d]: %c\n", j, buffer[j]);
|
||||
|
||||
switch(buffer[j]) {
|
||||
case 'q':
|
||||
printf("Quit command found at position %d\n", j);
|
||||
quit_server(self);
|
||||
return 1; // 立即退出,不处理后续命令
|
||||
case 'u':
|
||||
printf("Update command\n");
|
||||
// 更新逻辑
|
||||
break;
|
||||
default:
|
||||
printf("Unknown command: %c (ASCII: %d)\n",
|
||||
buffer[j], buffer[j]);
|
||||
}
|
||||
}
|
||||
} else if (bytes_read == 0) {
|
||||
printf("Pipe closed by writer\n");
|
||||
close(fifo_fd);
|
||||
} else if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||
perror("Error reading from pipe");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -307,7 +285,6 @@ void *run_network(void *self_d)
|
||||
{
|
||||
netm *self = (netm*)self_d;
|
||||
self->start_pool(self);
|
||||
self->statue = POOL_ON;
|
||||
server_run(self->port,self->fifo_fd[0],self);
|
||||
self->shutdown_pool(self);
|
||||
}
|
||||
@ -326,6 +303,5 @@ int init_networkmanager(netm *self,int *fifo,log_manager *logmanager,int port)
|
||||
//初始化参数
|
||||
self->logmanager = logmanager;
|
||||
self->err_indictor = (indiector*)malloc(sizeof(indiector));
|
||||
self->statue = ALL_STOP;
|
||||
return 0;
|
||||
}
|
||||
5
c/network/network.h
Executable file → Normal file
5
c/network/network.h
Executable file → Normal file
@ -1,9 +1,6 @@
|
||||
#ifndef NETWORK
|
||||
#define NETWORK
|
||||
|
||||
#define POOL_ON 1
|
||||
#define SERVER_ON 2
|
||||
#define ALL_STOP 0
|
||||
|
||||
#include <pthread.h>
|
||||
#include "tools/log/log.h"
|
||||
@ -32,14 +29,12 @@ typedef struct network_manager//网络管理器
|
||||
int (*iss_work)(struct network_manager*,char *);
|
||||
|
||||
int fifo_fd[2];
|
||||
pthread_t pid;
|
||||
log_manager *logmanager;
|
||||
indiector *err_indictor;
|
||||
int last_alc;
|
||||
int port;
|
||||
int epoll_fd;
|
||||
int http_fd;
|
||||
int statue;
|
||||
}netm;
|
||||
|
||||
typedef struct rebot_message
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
#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
Executable file → Normal file
2
c/network/swap.c
Executable file → Normal 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 = MEM_FREE;
|
||||
init_msg->state = FREE;
|
||||
init_msg->uid[0] = '\0';
|
||||
munmap((void*)init_msg,sizeof(rbt_msg));
|
||||
return fd;
|
||||
|
||||
2
c/network/swap.h
Executable file → Normal file
2
c/network/swap.h
Executable file → Normal file
@ -4,7 +4,7 @@
|
||||
#include "config.h"
|
||||
#define QUITPLG 0
|
||||
#define NEWMSG 1
|
||||
#define SW_FREE 2
|
||||
#define FREE 2
|
||||
|
||||
int make_swap(rbt_msg *swap);
|
||||
int create_swap(const char *name);
|
||||
|
||||
0
c/run_pluginmanager/run_pluginmanager.c
Executable file → Normal file
0
c/run_pluginmanager/run_pluginmanager.c
Executable file → Normal file
0
c/run_pluginmanager/run_pluginmanager.h
Executable file → Normal file
0
c/run_pluginmanager/run_pluginmanager.h
Executable file → Normal file
15
c/tem/ctl.c
Executable file → Normal file
15
c/tem/ctl.c
Executable file → Normal file
@ -241,7 +241,9 @@ int teml(Ctl *self,int fifo[2])
|
||||
char input[TEM_MAX_BUF] = {'\0'};
|
||||
ctx *command = (ctx*)malloc(sizeof(ctx));
|
||||
Cmd cmd_dir[10];
|
||||
init_interpreter(cmd_dir,command,fifo,self->logmanager);
|
||||
init_interpreter(cmd_dir,command,fifo,self->logmanager);//初始化解释器
|
||||
//创建线程用于定期清理日志
|
||||
pthread_create(&self->logwathcher,NULL,self->logmanager->clear_log,self->logmanager);
|
||||
command->statue = 0;
|
||||
self->command = command;
|
||||
do
|
||||
@ -252,13 +254,20 @@ int teml(Ctl *self,int fifo[2])
|
||||
perror("sys error");
|
||||
//将用户输入入队
|
||||
infifo(self,input);
|
||||
|
||||
self->logmanager->in_log(self->logmanager,input,"USER:");
|
||||
logs *log = (logs*)malloc(sizeof(logs));
|
||||
memcpy(log->log,input,sizeof(input));
|
||||
self->logmanager->in_log(log,self->logmanager);
|
||||
memcpy(command->command,input,sizeof(input));
|
||||
interpret(SIG_MOD,command,cmd_dir);
|
||||
const char fexp[256] = {'\0'};
|
||||
memcpy(&input,&fexp,TEM_MAX_BUF);
|
||||
}while(command->statue == 0);
|
||||
log_manager_stop(self->logmanager);
|
||||
pthread_join(self->logwathcher,NULL);
|
||||
//关闭log定期清理程序
|
||||
close(fifo[0]);
|
||||
|
||||
close(fifo[1]);
|
||||
free_history(self);
|
||||
self->command = NULL;
|
||||
free(command);
|
||||
|
||||
1
c/tem/ctl.h
Executable file → Normal file
1
c/tem/ctl.h
Executable file → Normal file
@ -15,6 +15,7 @@ typedef struct Ctl
|
||||
int (*infifo)(struct Ctl*,const char*);
|
||||
int index;
|
||||
char *history[TEM_HISTORY_BUF];
|
||||
pthread_t logwathcher;
|
||||
log_manager *logmanager;
|
||||
ctx *command;//解释器上下文
|
||||
toml_table_t *config;
|
||||
|
||||
188
c/tools/log/log.c
Executable file → Normal file
188
c/tools/log/log.c
Executable file → Normal file
@ -9,77 +9,42 @@
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
logs* getbody(void **log_p)
|
||||
int in_log(logs *log,log_manager *self)
|
||||
{
|
||||
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;
|
||||
if(*length+strlen(org)<maxlength-1)
|
||||
{
|
||||
strcpy(&writein[*length],org);
|
||||
*length +=strlen(org);//栈内存充足
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
int in_log(log_manager *self,const char *logbody,const char *info)
|
||||
{
|
||||
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);//加锁
|
||||
logs *buf = self->rear;
|
||||
if(self->log == NULL){
|
||||
self->log = log_p;
|
||||
self->rear = getbody(log_p);
|
||||
atomic_fetch_add(&self->count,1);
|
||||
self->log = log;
|
||||
self->rear = log;
|
||||
self->count++;
|
||||
sem_post(&self->log_sem);
|
||||
return self->count;
|
||||
}
|
||||
if(self->count == 1){
|
||||
logs *p = getbody(self->log);
|
||||
p->next = log_p;
|
||||
return 0;
|
||||
}
|
||||
self->count++;
|
||||
buf->next = log;
|
||||
log->next = NULL;
|
||||
self->rear->next = log_p;
|
||||
self->rear = log;
|
||||
sem_post(&self->log_sem);
|
||||
return self->count;
|
||||
}
|
||||
|
||||
logs *out_log(log_manager *self)
|
||||
{
|
||||
sem_wait(&self->log_sem);
|
||||
logs *buf = self->log;
|
||||
if(self->log==NULL)
|
||||
{
|
||||
sem_post(&self->log_sem);
|
||||
return NULL;
|
||||
}
|
||||
if(self->log->next ==NULL)
|
||||
self->log = self->rear = NULL;
|
||||
self->count--;
|
||||
sem_post(&self->log_sem);
|
||||
buf->next =NULL;
|
||||
return buf;
|
||||
}
|
||||
|
||||
int sleep_with_signal(log_manager *self)
|
||||
{
|
||||
struct timespec ts;
|
||||
@ -89,7 +54,7 @@ int sleep_with_signal(log_manager *self)
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
|
||||
return -1; /* 罕见失败 */
|
||||
|
||||
ts.tv_sec += LOG_SLEEP_LENGTH;
|
||||
ts.tv_sec += 1000;
|
||||
/* 纳秒部分无需处理,1000 s 整不会溢出 */
|
||||
|
||||
pthread_mutex_lock(&self->mtx); /* 进入临界区 */
|
||||
@ -118,74 +83,35 @@ int cleanup(log_manager *self)
|
||||
if(self->log ==NULL)
|
||||
return 1;
|
||||
logs *tobeclean,*loc;
|
||||
void **tobeclean_p;
|
||||
sem_wait(&self->log_sem);//获取信号量
|
||||
void **loc_p = self->log;
|
||||
|
||||
loc = self->log;
|
||||
self->log = NULL;
|
||||
atomic_store(&self->count,0);//摘取log链
|
||||
self->count = 0;//摘取log链
|
||||
|
||||
sem_post(&self->log_sem);
|
||||
//释放信号量
|
||||
loc = getbody(loc_p);
|
||||
|
||||
char logbuf[MAX_LOG_LENGTH];
|
||||
|
||||
size_t buf_length = 0;
|
||||
int fd;
|
||||
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0777);
|
||||
while(loc->next !=NULL)
|
||||
{
|
||||
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");//非业务逻辑只警告
|
||||
|
||||
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");
|
||||
|
||||
self->mempool->FreeBlock(self,loc_p);
|
||||
|
||||
fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd == -1){
|
||||
perror("log:");
|
||||
}
|
||||
int error_buf = write(fd,logbuf,strlen(logbuf));
|
||||
if(error_buf==-1){
|
||||
return -1;
|
||||
}
|
||||
else if(error_buf<strlen(logbuf)){
|
||||
perror("file");
|
||||
write(fd,"unknown error case log write cut down\n",38);
|
||||
tobeclean = loc;
|
||||
loc = loc->next;
|
||||
if(fd == -1)
|
||||
perror("file:");
|
||||
write(fd,tobeclean->log,strlen(tobeclean->log));
|
||||
free(tobeclean);
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
free(loc);
|
||||
}
|
||||
|
||||
void log_manager_stop(log_manager *self)
|
||||
{
|
||||
pthread_mutex_lock(&self->mtx);
|
||||
if(self->stop == 1){
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
return ;
|
||||
}
|
||||
self->stop = 1;
|
||||
/* 置退出标志 */
|
||||
printf("SYS:stopping loger\n");
|
||||
self->in_log(self,"stopping loger\n","SYS:");
|
||||
printf("SYS:done\n");
|
||||
self->in_log(self,"done","SYS:");
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
self->stop = 1; /* 置退出标志 */
|
||||
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
|
||||
printf("SYS:stopping loger\n");
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
printf("SYS:done\n");
|
||||
}
|
||||
|
||||
//定期清理函数
|
||||
@ -196,7 +122,7 @@ void *clear_log(void *self_p)
|
||||
{
|
||||
sleep_with_signal(self);
|
||||
sem_wait(&self->log_sem);
|
||||
if((self->count<MAX_LOG||self->log==NULL)&&self->stop !=1){
|
||||
if((self->count<256||self->log==NULL)&&self->stop !=1){
|
||||
sem_post(&self->log_sem);
|
||||
continue;
|
||||
}
|
||||
@ -208,41 +134,21 @@ void *clear_log(void *self_p)
|
||||
}
|
||||
}
|
||||
|
||||
int init_loger(log_manager *self,mem_ctl *mempool)
|
||||
int init_loger(log_manager *self)
|
||||
{
|
||||
if(self == NULL)
|
||||
{
|
||||
perror("NULL\n");
|
||||
return -1;
|
||||
}
|
||||
if(sem_init(&self->log_sem, 0,1)==-1)
|
||||
return -1;
|
||||
if(pthread_mutex_init(&self->mtx,NULL)==-1)
|
||||
{
|
||||
if(sem_destroy(&self->log_sem)==-1)
|
||||
{
|
||||
perror("log:");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if(pthread_cond_init(&self->cond,NULL)==-1)
|
||||
{
|
||||
if(sem_destroy(&self->log_sem)==-1)
|
||||
{
|
||||
perror("log:");
|
||||
}
|
||||
if(pthread_mutex_destroy(&self->mtx)==-1)
|
||||
{
|
||||
perror("log:");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
sem_init(&self->log_sem, 0,1);
|
||||
pthread_mutex_init(&self->mtx,NULL);
|
||||
pthread_cond_init(&self->cond,NULL);
|
||||
self->in_log = in_log;
|
||||
self->out_log = out_log;
|
||||
self->clear_log = clear_log;
|
||||
self->log = NULL;
|
||||
self->stop = 0;
|
||||
self->cleanup = cleanup;
|
||||
atomic_init(&self->count,0);
|
||||
self->mempool = mempool;
|
||||
return 0;
|
||||
self->count = 0;
|
||||
}
|
||||
17
c/tools/log/log.h
Executable file → Normal file
17
c/tools/log/log.h
Executable file → Normal file
@ -2,35 +2,32 @@
|
||||
#define LOG
|
||||
|
||||
#include "config.h"
|
||||
#include "memctl/memctl.h"
|
||||
#include <semaphore.h>
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
typedef struct logs
|
||||
{
|
||||
char log[MAX_LOG_LENGTH];
|
||||
void **next;
|
||||
char info[INFO_LENGTH];
|
||||
char log[4096];
|
||||
struct logs *next;
|
||||
}logs;
|
||||
|
||||
typedef struct log_manager
|
||||
{
|
||||
pthread_t pid;
|
||||
mem_ctl *mempool;
|
||||
int (*in_log)(struct log_manager*,const char *,const char *);
|
||||
int (*in_log)(logs *,struct log_manager*);
|
||||
logs* (*out_log)(struct log_manager*);
|
||||
void *(*clear_log)(void*);
|
||||
int (*cleanup)(struct log_manager*);
|
||||
sem_t log_sem;
|
||||
void **log;
|
||||
logs *log;
|
||||
logs *rear;
|
||||
atomic_int count;
|
||||
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,mem_ctl *mempool);
|
||||
int init_loger(log_manager *self);
|
||||
|
||||
#endif
|
||||
0
c/tools/pkgmanager/pkginstall.c
Executable file → Normal file
0
c/tools/pkgmanager/pkginstall.c
Executable file → Normal file
0
c/tools/pkgmanager/pkginstall.h
Executable file → Normal file
0
c/tools/pkgmanager/pkginstall.h
Executable file → Normal file
0
c/tools/pkgmanager/update_pkg.c
Executable file → Normal file
0
c/tools/pkgmanager/update_pkg.c
Executable file → Normal file
0
c/tools/pkgmanager/update_pkg.h
Executable file → Normal file
0
c/tools/pkgmanager/update_pkg.h
Executable file → Normal file
43
c/tools/quit/quit.c
Executable file → Normal file
43
c/tools/quit/quit.c
Executable file → Normal file
@ -11,62 +11,46 @@
|
||||
#include "tem/ctl.h"
|
||||
#include "tools/toml/toml.h"
|
||||
|
||||
|
||||
int quit_server(netm *self)
|
||||
{
|
||||
if(self ==NULL)
|
||||
return -1;
|
||||
|
||||
//关闭epoll监听
|
||||
if(self->epoll_fd != -1)
|
||||
{
|
||||
epoll_ctl(self->epoll_fd,EPOLL_CTL_DEL,self->http_fd,NULL);
|
||||
epoll_ctl(self->epoll_fd,EPOLL_CTL_DEL,self->fifo_fd[0],NULL);
|
||||
self->epoll_fd = -1;
|
||||
}
|
||||
//关闭socket监听
|
||||
//关闭epoll监听
|
||||
if(self->http_fd != -1)
|
||||
{
|
||||
shutdown(self->http_fd, SHUT_RDWR);
|
||||
if(close(self->http_fd)==-1)
|
||||
perror("http");
|
||||
return -1;
|
||||
self->http_fd =-1;
|
||||
}
|
||||
//关闭管道监听
|
||||
//关闭socket监听
|
||||
if(self->fifo_fd[1] != -1)
|
||||
{
|
||||
if(close(self->fifo_fd[1])==-1)
|
||||
return -1;
|
||||
self->fifo_fd[1] = -1;
|
||||
}
|
||||
|
||||
//关闭管道监听
|
||||
free(self->err_indictor);
|
||||
self->statue = POOL_ON;
|
||||
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);
|
||||
|
||||
|
||||
if(resouce->network->statue == SERVER_ON)
|
||||
{
|
||||
quit_server(resouce->network);
|
||||
}
|
||||
if(resouce->network->statue == POOL_ON)
|
||||
{
|
||||
resouce->network->shutdown_pool(resouce->network);
|
||||
}
|
||||
resouce->loger->in_log(resouce->loger,"shutting down network pool","SYS:");
|
||||
logs *netlog = (logs*)malloc(sizeof(logs));
|
||||
netlog->next = NULL;
|
||||
memcpy(netlog->log,"shuting down networkserver",27);
|
||||
quit_server(resouce->network);
|
||||
resouce->loger->in_log(netlog,resouce->loger);
|
||||
free(resouce->network);
|
||||
//释放网络资源
|
||||
if(resouce->tem->command !=NULL){
|
||||
@ -88,18 +72,11 @@ void quit_all(int status,void *self_p)
|
||||
free(resouce->tem->command);
|
||||
}
|
||||
//释放终端资源
|
||||
//释放日志管理器
|
||||
if(resouce->loger->pid != -1){
|
||||
log_manager_stop(resouce->loger);
|
||||
pthread_join(resouce->loger->pid,NULL);
|
||||
}
|
||||
pthread_mutex_destroy(&resouce->loger->mtx);
|
||||
pthread_cond_destroy(&resouce->loger->cond);
|
||||
log_manager_stop(resouce->loger);
|
||||
resouce->loger->cleanup(resouce->loger);
|
||||
sem_destroy(&resouce->loger->log_sem);
|
||||
//销毁信号量
|
||||
|
||||
free(resouce->loger);
|
||||
//清理日志
|
||||
free(resouce);
|
||||
}
|
||||
|
||||
3
c/tools/quit/quit.h
Executable file → Normal file
3
c/tools/quit/quit.h
Executable file → Normal file
@ -4,14 +4,11 @@
|
||||
#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;
|
||||
|
||||
|
||||
0
c/tools/toml/toml.c
Executable file → Normal file
0
c/tools/toml/toml.c
Executable file → Normal file
0
c/tools/toml/toml.h
Executable file → Normal file
0
c/tools/toml/toml.h
Executable file → Normal file
0
config/config.toml
Executable file → Normal file
0
config/config.toml
Executable file → Normal file
0
src/file_store_api.py
Executable file → Normal file
0
src/file_store_api.py
Executable file → Normal file
0
src/modules/__init__.py
Executable file → Normal file
0
src/modules/__init__.py
Executable file → Normal file
0
src/modules/plugin_modules.py
Executable file → Normal file
0
src/modules/plugin_modules.py
Executable file → Normal file
0
src/modules/user_modules.py
Executable file → Normal file
0
src/modules/user_modules.py
Executable file → Normal file
0
src/plugin_manager.py
Executable file → Normal file
0
src/plugin_manager.py
Executable file → Normal file
Reference in New Issue
Block a user