Compare commits

..

10 Commits

41 changed files with 455 additions and 270 deletions

2
.gitignore vendored Normal file → Executable file
View File

@ -6,7 +6,7 @@ __pycache__/
# C extensions
*.so
.vscode/
# Distribution / packaging
.Python
build/

0
LICENSE Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

3
c/CMakeLists.txt Normal file → Executable file
View 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(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)
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit Memctl)
include_directories(${PROJECT_SOURCE_DIR})

23
c/config.h Normal file → Executable file
View File

@ -1,18 +1,37 @@
#ifndef 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_HISTORY_BUF 210
#define TEM_PROMPT "chatbot$$ "
/*----终端管理--------*/
/*-------网路池管理-----*/
#define NET_MAX_POOL 1
#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

30
c/interpreter/interpreter.c Normal file → Executable file
View File

@ -9,24 +9,22 @@
#include "interpreter.h"
#include "tools/pkgmanager/pkginstall.h"
int inter_in_log(const char *log,log_manager *manager)
int inter_in_log(const char *log,const char *info,log_manager *manager)
{
if(strlen(log)>1024)
return -1;
logs* logss = (logs*)malloc(sizeof(logs));
memcpy(logss->log,log,strlen(log));
manager->in_log(logss,manager);
manager->in_log(manager,log,info);
return 0;
}
int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager)
{
printf("SYS:prepare env\n");
inter_in_log("SYS:prepare env\n",log_manager);
inter_in_log("prepare env\n","SYS",log_manager);
printf("SYS:env ready\n");
inter_in_log("SYS:env ready\n",log_manager);
inter_in_log("env ready\n","SYS",log_manager);
printf("SYS:loading cmd_dic\n");
inter_in_log("SYS:loading cmd_dic\n",log_manager);
inter_in_log("loading cmd_dic\n","SYS",log_manager);
sprintf(cmd_dic[0].name, "pkginstall");
cmd_dic[0].cmd = INSTALL;
@ -37,7 +35,7 @@ int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager
cmd_dic[2].cmd = QUIT;
printf("SYS:cmd_dir load complite\n");
inter_in_log("SYS:cmd_dir load complite\n",log_manager);
inter_in_log("cmd_dir load complite\n","SYS",log_manager);
for(int i =0;i<10;i++)
{
@ -45,7 +43,7 @@ int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager
}
self->arg = NULL;
printf("SYS:Creating ctl fifo\n");
inter_in_log("SYS:Creating ctl fifo\n",log_manager);
inter_in_log("Creating ctl fifo\n","SYS",log_manager);
memcpy(self->fifofd,fifo,2*sizeof(int));
self->log_manager = log_manager;
}
@ -71,7 +69,7 @@ int get_args(ctx *self)
arg->next = (args*)malloc(sizeof(args));
if(arg->next == NULL){
perror("ERROR:fail to get mem");
inter_in_log("ERROR:fail to get mem\n",self->log_manager);
inter_in_log("fail to get mem\n","ERROR",self->log_manager);
return -1;
}
arg = arg->next;
@ -146,31 +144,31 @@ int exec(const int command,ctx *all_ctx)
{
case BAD_INPUT:
printf("SYS:bad input,try again\n");
inter_in_log("SYS:bad input,try again\n",all_ctx->log_manager);
inter_in_log("bad input,try again\n","SYS",all_ctx->log_manager);
return BAD_INPUT;
case INSTALL:
if(all_ctx->arg == NULL){
printf("SYS:Missing args\n");
inter_in_log("SYS:Missng args\n",all_ctx->log_manager);
inter_in_log("Missng args\n","SYS",all_ctx->log_manager);
return 1;
}
printf("SYS:init pkgmanager\n");
inter_in_log("SYS:init pkgmanager\n",all_ctx->log_manager);
inter_in_log("init pkgmanager\n","SYS",all_ctx->log_manager);
pkger *manager = init_pkginstaller();
printf("SYS:installing\n");
inter_in_log("SYS:installing\n",all_ctx->log_manager);
inter_in_log("installing\n","SYS",all_ctx->log_manager);
manager->packup(manager);
return 1;
case RUN:
printf("SYS:runing\n");
inter_in_log("SYS:running\n",all_ctx->log_manager);
inter_in_log("running\n","SYS",all_ctx->log_manager);
return 1;
case QUIT:
printf("SYS:shuting down\n");
inter_in_log("SYS:shuting down\n",all_ctx->log_manager);
inter_in_log("shuting down\n","SYS",all_ctx->log_manager);
all_ctx->statue = -1;
write(all_ctx->fifofd[1],"q",1);
return 1;

0
c/interpreter/interpreter.h Normal file → Executable file
View File

16
c/main.c Normal file → Executable file
View File

@ -3,6 +3,8 @@
#include "network/network.h"
#include "tools/toml/toml.h"
#include "tools/quit/quit.h"
#include "memctl/memctl.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
@ -33,9 +35,11 @@ int main()
perror("load config error");
int port = (int)toml_int_in(server,"list_port").u.i;
//加载配置文件,读取端口
mem_ctl *mem_ctler = (mem_ctl*)malloc(sizeof(mem_ctl));
init_memctl(mem_ctler);
log_manager *logsmanager=(log_manager*)malloc(sizeof(log_manager));
//创建日志管理器与定时清理线层
init_loger(logsmanager);
init_loger(logsmanager,mem_ctler);
pthread_create(&logsmanager->pid,NULL,logsmanager->clear_log,logsmanager);
Ctl *teml = init_tem(logsmanager);
teml->config = server;
@ -53,18 +57,20 @@ int main()
resource->loger = logsmanager;
resource->network = networkmanager;
resource->tem = teml;
resource->memctler = mem_ctler;
on_exit(quit_all,resource);
//注册清理函数
teml->run(teml,fifo);
//启动终端
//等待网络管理器进程结束
pthread_join(networkmanager->pid,NULL);
networkmanager->pid = -1;
close(fifo[1]);
log_manager_stop(logsmanager);
pthread_join(logsmanager->pid,NULL);
logsmanager->pid = -1;
//等待网络管理器进程结束
pthread_join(networkmanager->pid,NULL);
close(fifo[1]);
return 1;
}

159
c/memctl/memctl.c Normal file
View 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
View 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
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(""))

0
c/network/cJSON.h Normal file → Executable file
View File

0
c/network/erroprocess/erroprocess.c Normal file → Executable file
View File

0
c/network/erroprocess/erroprocess.h Normal file → Executable file
View File

123
c/network/http/http_rel.c Normal file → Executable file
View File

@ -5,71 +5,18 @@
#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;
}
@ -81,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;
}
@ -134,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;
}

3
c/network/http/http_rel.h Normal file → Executable file
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);

23
c/network/network.c Normal file → Executable file
View 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)
{
if(json_text == NULL)
return -1;
memset(out, 0, sizeof(*out)); // 统一清 0gid 天然 '\0'
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);
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 =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
@ -163,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(;;){
@ -183,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);
}
@ -217,6 +216,7 @@ int shutdown_pool(netm *self)
self->pool[i].status = -1;
close(self->pool[i].fifo_fd[1]);
}
self->statue = ALL_STOP;
return 1;
}
@ -239,6 +239,7 @@ 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(;;)
{
/*工作循环-----------------------------*/
@ -306,6 +307,7 @@ 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);
}
@ -324,5 +326,6 @@ 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;
}

4
c/network/network.h Normal file → Executable file
View File

@ -1,6 +1,9 @@
#ifndef NETWORK
#define NETWORK
#define POOL_ON 1
#define SERVER_ON 2
#define ALL_STOP 0
#include <pthread.h>
#include "tools/log/log.h"
@ -36,6 +39,7 @@ typedef struct network_manager//网络管理器
int port;
int epoll_fd;
int http_fd;
int statue;
}netm;
typedef struct rebot_message

17
c/network/protocal.h Normal file
View 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
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;

2
c/network/swap.h Normal file → Executable file
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);

0
c/run_pluginmanager/run_pluginmanager.c Normal file → Executable file
View File

0
c/run_pluginmanager/run_pluginmanager.h Normal file → Executable file
View File

5
c/tem/ctl.c Normal file → Executable file
View File

@ -252,9 +252,8 @@ int teml(Ctl *self,int fifo[2])
perror("sys error");
//将用户输入入队
infifo(self,input);
logs *log = (logs*)malloc(sizeof(logs));
memcpy(log->log,input,sizeof(input));
self->logmanager->in_log(log,self->logmanager);
self->logmanager->in_log(self->logmanager,input,"USER:");
memcpy(command->command,input,sizeof(input));
interpret(SIG_MOD,command,cmd_dir);
const char fexp[256] = {'\0'};

0
c/tem/ctl.h Normal file → Executable file
View File

207
c/tools/log/log.c Normal file → Executable file
View File

@ -9,50 +9,77 @@
#include <fcntl.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;
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);//加锁
if(self->log == NULL){
self->log = log;
self->rear = log;
self->count++;
self->log = log_p;
self->rear = getbody(log_p);
atomic_fetch_add(&self->count,1);
sem_post(&self->log_sem);
return self->count;
}
if(self->count == 1)
self->log->next = log;
if(self->count == 1){
logs *p = getbody(self->log);
p->next = log_p;
}
self->count++;
log->next = NULL;
self->rear->next = log;
self->rear->next = log_p;
self->rear = log;
sem_post(&self->log_sem);
return self->count;
}
logs *out_log(log_manager *self)
{
if(self == NULL)
return NULL;
sem_wait(&self->log_sem);
logs *buf = self->log;
if(buf==NULL)
{
sem_post(&self->log_sem);
return NULL;
}
if(self->log->next ==NULL)
self->log = self->rear = NULL;
else
self->log = self->log->next;
self->count--;
sem_post(&self->log_sem);
buf->next =NULL;
return buf;
}
int sleep_with_signal(log_manager *self)
{
struct timespec ts;
@ -62,7 +89,7 @@ int sleep_with_signal(log_manager *self)
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
return -1; /* 罕见失败 */
ts.tv_sec += 1000;
ts.tv_sec += LOG_SLEEP_LENGTH;
/* 纳秒部分无需处理1000 s 整不会溢出 */
pthread_mutex_lock(&self->mtx); /* 进入临界区 */
@ -91,90 +118,56 @@ int cleanup(log_manager *self)
if(self->log ==NULL)
return 1;
logs *tobeclean,*loc;
void **tobeclean_p;
sem_wait(&self->log_sem);//获取信号量
loc = self->log;
void **loc_p = self->log;
self->log = NULL;
self->count = 0;//摘取log链
atomic_store(&self->count,0);//摘取log链
sem_post(&self->log_sem);
//释放信号量
char *logbuf;
void *logbufbk;
char failback[MAX_LOG_LENGTH];
logbuf = (char*)malloc(1);
logbuf[0] = '\0';
size_t buf_length,buf_lengthbk = 0;
loc = getbody(loc_p);
char logbuf[MAX_LOG_LENGTH];
size_t buf_length = 0;
int fd;
while(loc->next !=NULL)
{
tobeclean = loc;
loc = loc->next;
if(logbuf != failback){
logbufbk = logbuf;
logbuf = (char*)realloc(logbuf,strlen(logbuf)+strlen(tobeclean->log)+1);//为日志分配新的内存
}
if(logbuf == NULL){
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);
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);
}
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
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("file:");
free(loc);
if(logbuf != failback)
free(logbuf);
return -1;
perror("log:");
}
if(logbuf != failback){
logbufbk = logbuf;
logbuf = (char*)realloc(logbuf,strlen(logbuf)+strlen(loc->log)+2);
if(logbuf == NULL)
{
free(logbufbk);
logbuf = failback;
strcpy(logbuf,loc->log);
buf_length = strlen(loc->log);
}
else{
buf_lengthbk = buf_length;
buf_length = strlen(logbuf)+strlen(loc->log);
strcpy(logbuf+buf_lengthbk,loc->log);
}
}
else{
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);
int error_buf = write(fd,logbuf,strlen(logbuf));
if(error_buf==-1){
close(fd);
if(logbuf != failback)
free(logbuf);
return -1;
}
else if(error_buf<buf_length)
else if(error_buf<strlen(logbuf)){
perror("file");
write(fd,"unknown error case log write cut down\n",38);
if(logbuf != failback)
free(logbuf);
close(fd);
}
close(fd);
return 0;
}
@ -188,13 +181,9 @@ void log_manager_stop(log_manager *self)
self->stop = 1;
/* 置退出标志 */
printf("SYS:stopping loger\n");
logs *log = malloc(sizeof(logs));
strcpy(log->log,"SYS:stopping loger\n");
self->in_log(log,self);
log = malloc(sizeof(logs));
strcpy(log->log,"SYS:done\n");
self->in_log(self,"stopping loger\n","SYS:");
printf("SYS:done\n");
self->in_log(log,self);
self->in_log(self,"done","SYS:");
pthread_mutex_unlock(&self->mtx);
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
}
@ -219,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)
{
@ -249,11 +238,11 @@ int init_loger(log_manager *self)
return -1;
}
self->in_log = in_log;
self->out_log = out_log;
self->clear_log = clear_log;
self->log = NULL;
self->stop = 0;
self->cleanup = cleanup;
self->count = 0;
atomic_init(&self->count,0);
self->mempool = mempool;
return 0;
}

14
c/tools/log/log.h Normal file → Executable file
View File

@ -2,6 +2,7 @@
#define LOG
#include "config.h"
#include "memctl/memctl.h"
#include <semaphore.h>
#include <pthread.h>
@ -9,26 +10,27 @@
typedef struct logs
{
char log[MAX_LOG_LENGTH];
struct logs *next;
void **next;
char info[INFO_LENGTH];
}logs;
typedef struct log_manager
{
pthread_t pid;
int (*in_log)(logs *,struct log_manager*);
logs* (*out_log)(struct log_manager*);
mem_ctl *mempool;
int (*in_log)(struct log_manager*,const char *,const char *);
void *(*clear_log)(void*);
int (*cleanup)(struct log_manager*);
sem_t log_sem;
logs *log;
void **log;
logs *rear;
int count;
atomic_int count;
pthread_mutex_t mtx;
pthread_cond_t cond;
int stop;
}log_manager;
void log_manager_stop(log_manager *self);
int init_loger(log_manager *self);
int init_loger(log_manager *self,mem_ctl *mempool);
#endif

0
c/tools/pkgmanager/pkginstall.c Normal file → Executable file
View File

0
c/tools/pkgmanager/pkginstall.h Normal file → Executable file
View File

0
c/tools/pkgmanager/update_pkg.c Normal file → Executable file
View File

0
c/tools/pkgmanager/update_pkg.h Normal file → Executable file
View File

35
c/tools/quit/quit.c Normal file → Executable file
View File

@ -11,46 +11,62 @@
#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;
}
//关闭epoll监听
//关闭socket监听
if(self->http_fd != -1)
{
shutdown(self->http_fd, SHUT_RDWR);
if(close(self->http_fd)==-1)
return -1;
perror("http");
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);
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);
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:");
free(resouce->network);
//释放网络资源
if(resouce->tem->command !=NULL){
@ -85,4 +101,5 @@ void quit_all(int status,void *self_p)
free(resouce->loger);
//清理日志
free(resouce);
}

3
c/tools/quit/quit.h Normal file → Executable file
View File

@ -4,11 +4,14 @@
#include "network/network.h"
#include "tem/ctl.h"
#include "tools/log/log.h"
#include "memctl/memctl.h"
typedef struct all_resources
{
Ctl *tem;
netm *network;
log_manager *loger;
mem_ctl *memctler;
}alres;

0
c/tools/toml/toml.c Normal file → Executable file
View File

0
c/tools/toml/toml.h Normal file → Executable file
View File

0
config/config.toml Normal file → Executable file
View File

0
src/file_store_api.py Normal file → Executable file
View File

0
src/modules/__init__.py Normal file → Executable file
View File

0
src/modules/plugin_modules.py Normal file → Executable file
View File

0
src/modules/user_modules.py Normal file → Executable file
View File

0
src/plugin_manager.py Normal file → Executable file
View File