Files
chat_rebot-connect-with-one…/c/tools/quit/quit.c
2026-01-29 14:03:43 +08:00

101 lines
2.5 KiB
C

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include "quit.h"
#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监听
if(self->http_fd != -1)
{
shutdown(self->http_fd, SHUT_RDWR);
if(close(self->http_fd)==-1)
perror("http");
self->http_fd =-1;
}
//关闭管道监听
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;
}
void quit_all(int status,void *self_p)
{
alres *resouce =(alres*)self_p;
//转换参数
resouce->network->shutdown_pool(resouce->network);
logs *netlog = (logs*)malloc(sizeof(logs));
netlog->next = NULL;
memcpy(netlog->log,"shuting down networkserver",27);
if(resouce->network->statue == SERVER_ON)
{
quit_server(resouce->network);
}
if(resouce->network->statue == POOL_ON)
{
resouce->network->shutdown_pool(resouce->network);
}
resouce->loger->in_log(netlog,resouce->loger);
free(resouce->network);
//释放网络资源
if(resouce->tem->command !=NULL){
free_history(resouce->tem);
if(resouce->tem->command->arg != NULL)
{
args* arg = resouce->tem->command->arg;
if(arg->next !=NULL)
{
while(arg->next != NULL){
args* tobefree = arg;
arg = arg->next;
free(tobefree);
}
free(arg);
}
}
toml_free(resouce->tem->config);
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);
sem_destroy(&resouce->loger->log_sem);
//销毁信号量
free(resouce->loger);
//清理日志
}