Now handle signals
This commit is contained in:
parent
8b6f5c63f6
commit
163110c814
7 changed files with 66 additions and 1 deletions
|
@ -77,6 +77,10 @@ void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp) {
|
|||
};
|
||||
|
||||
evt_core_init (&(ctx->evts), dp->verbose);
|
||||
|
||||
signal_init(&ctx->evts);
|
||||
printf("--- Signal initialized\n");
|
||||
|
||||
algo_main_init(&ctx->evts, &ap);
|
||||
printf("--- Algorithm initialized\n");
|
||||
|
||||
|
|
|
@ -8,6 +8,57 @@ void free_udp_t(void* v) {
|
|||
}
|
||||
}
|
||||
|
||||
int on_signal(struct evt_core_ctx* evts, struct evt_core_fdinfo* fdinfo) {
|
||||
struct signalfd_siginfo fdsi;
|
||||
|
||||
int sig_rd = read(fdinfo->fd, &fdsi, sizeof(struct signalfd_siginfo));
|
||||
if (sig_rd == -1 && errno == EAGAIN) return 1;
|
||||
if (sig_rd != sizeof(struct signalfd_siginfo)) {
|
||||
fprintf(stderr, "read size: %d / %ld\n", sig_rd, sizeof(struct signalfd_siginfo));
|
||||
perror("signal read failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Signal received: %d\n", fdsi.ssi_signo);
|
||||
|
||||
if (fdsi.ssi_signo == SIGINT || fdsi.ssi_signo == SIGQUIT) {
|
||||
printf("Stop main loop and quit\n");
|
||||
evts->loop = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void signal_init(struct evt_core_ctx* evts) {
|
||||
sigset_t mask;
|
||||
|
||||
struct evt_core_cat signal_read = {
|
||||
.name = "signal-read",
|
||||
.flags = EPOLLIN | EPOLLET | EPOLLRDHUP,
|
||||
.app_ctx = NULL,
|
||||
.free_app_ctx = NULL,
|
||||
.cb = on_signal,
|
||||
.err_cb = NULL
|
||||
};
|
||||
evt_core_add_cat(evts, &signal_read);
|
||||
|
||||
sigaddset(&mask, SIGINT);
|
||||
sigaddset(&mask, SIGQUIT);
|
||||
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
|
||||
perror("sigprocmask");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int sigfd = signalfd(-1, &mask, 0);
|
||||
if (sigfd == -1) {
|
||||
perror("signalfd failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct evt_core_fdinfo fdinfo = {0};
|
||||
fdinfo.fd = sigfd;
|
||||
fdinfo.cat = &signal_read;
|
||||
fdinfo.url = "signal:read:int+quit";
|
||||
evt_core_add_fd(evts, &fdinfo);
|
||||
}
|
||||
|
||||
void init_udp_remote(char* port, struct evt_core_ctx* evts) {
|
||||
int sock1, sock2;
|
||||
char url[1024];
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <errno.h>
|
||||
#include "net_tools.h"
|
||||
#include "evt_core.h"
|
||||
#include "packet.h"
|
||||
|
@ -12,6 +14,7 @@ struct donar_params {
|
|||
GPtrArray *remote_ports, *exposed_ports;
|
||||
};
|
||||
|
||||
void signal_init(struct evt_core_ctx* evts);
|
||||
void init_udp_remote(char* port, struct evt_core_ctx* evts);
|
||||
void init_udp_exposed(char* port, struct evt_core_ctx* evts);
|
||||
void donar_init_params(struct donar_params* dp);
|
||||
|
|
|
@ -59,6 +59,10 @@ void donar_server(struct donar_server_ctx* ctx, struct donar_params* dp) {
|
|||
};
|
||||
|
||||
evt_core_init (&(ctx->evts), dp->verbose);
|
||||
|
||||
signal_init(&ctx->evts);
|
||||
printf("--- Signal initialized\n");
|
||||
|
||||
algo_main_init(&ctx->evts, &ap);
|
||||
printf("--- Algorithm initialized\n");
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ void evt_core_init(struct evt_core_ctx* ctx, uint8_t verbose) {
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ctx->verbose = verbose;
|
||||
ctx->loop = 1;
|
||||
ctx->catlist = g_hash_table_new_full(g_str_hash, g_str_equal,NULL, free_cat);
|
||||
ctx->socklist = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, free_fdinfo);
|
||||
ctx->urltofd = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
|
||||
|
@ -257,7 +258,7 @@ void evt_core_loop(struct evt_core_ctx* ctx) {
|
|||
|
||||
printf("--- Start main loop\n");
|
||||
int num_fd, n = 0;
|
||||
while(1) {
|
||||
while(ctx->loop) {
|
||||
timing_fx_start(&tfx_epoll);
|
||||
num_fd = epoll_wait(ctx->epollfd, events, EVT_CORE_MAX_EVENTS, -1);
|
||||
timing_fx_stop(&tfx_epoll);
|
||||
|
|
|
@ -32,6 +32,7 @@ struct evt_core_cat {
|
|||
struct evt_core_ctx {
|
||||
int epollfd;
|
||||
uint8_t verbose;
|
||||
uint8_t loop;
|
||||
GHashTable* catlist; // name -> category
|
||||
GHashTable* socklist; // fd -> category
|
||||
GHashTable* urltofd; // url -> fd, like "tcp:127.0.0.1:7500"
|
||||
|
|
|
@ -191,6 +191,7 @@ void algo_main_init(struct evt_core_ctx* evt, struct algo_params* ap) {
|
|||
.cb = main_on_tcp_co,
|
||||
.err_cb = NULL
|
||||
};
|
||||
ctx->ref_count++;
|
||||
evt_core_add_cat(evt, &tcp_listen);
|
||||
|
||||
struct evt_core_cat tcp_read = {
|
||||
|
|
Loading…
Reference in a new issue