Skip to content

Commit

Permalink
[DRAFT] Signal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed Apr 27, 2024
1 parent baf0da8 commit 4b97526
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LDFLAGS += $(call if_coverage,--coverage)
# Project configuration ########################################################
RL_C := wireprotocol.c frontend.c header.c body.c attrs.c frame.c
RL_O := $(RL_C:.c=.o)
CFILES := main.c log.c utils.c serialization.c autoresponder.c
CFILES := main.c log.c utils.c serialization.c autoresponder.c sighandler.c controller.c
OFILES := $(RL_O) $(CFILES:.c=.o)
UT_C := ut.c serialization.c log.c utils.c contextring.c
UT_O := $(UT_C:.c=.o) header.o body.o frame.o attrs.o
Expand Down
10 changes: 10 additions & 0 deletions controller.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "frob.h"
#include "log.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>

int controller(struct controller_args*) {
LOGWX("Master channel doesn't work: %s", strerror(ENOSYS));
return 0;
}
8 changes: 8 additions & 0 deletions frob.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,16 @@ struct autoresponder_args {
int out; // Output file descriptor
};

struct sighandler_args {
};

struct controller_args {
};

int fsm_wireformat(void*);
int fsm_frontend_foreign(struct fsm_frontend_foreign_args*);
int fsm_frontend_internal(struct fsm_frontend_internal_args*);
int fsm_frontend_timer(struct fsm_frontend_timer_args*);
int autoresponder(const struct autoresponder_args*);
int sighandler(struct sighandler_args*);
int controller(struct controller_args*);
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ int main(const int ac, const char* av[static const ac]) {
sus_registration(autoresponder, av[2], 1, fds[CHANNEL_FO_MAIN]),
sus_registration(fsm_wireformat, fds[CHANNEL_FI_MAIN]),
sus_registration(fsm_frontend_foreign),
sus_registration(sighandler),
sus_registration(controller),
sus_registration(sus_ioloop, .timeout = atoi(av[1]))
};
# if 0
Expand Down
51 changes: 51 additions & 0 deletions sighandler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "frob.h"
#include "utils.h"
#include "log.h"
#include "multitasking/sus.h"
#include <signal.h>
#include <stdio.h>
#include <sys/signalfd.h>

#ifndef SIGINFO
# define SIGINFO SIGPWR
#endif

static int setup_signalfd(const int ch, const sigset_t blocked) {
if (sigprocmask(SIG_BLOCK, &blocked, NULL) != 0)
EXITF("Couldn't adjust signal mask");
const int fd = signalfd(ch, &blocked, SFD_CLOEXEC);
if (fd == -1)
EXITF("Couldn't setup sigfd for %d", ch);
return fd;
}

static sigset_t adjust_signal_delivery(int* const ch) {
static const int blocked_signals[] = { SIGINFO, SIGINT };
sigset_t s;
sigemptyset(&s);
for (size_t i = 0; i < elementsof(blocked_signals); i++)
sigaddset(&s, blocked_signals[i]);
*ch = setup_signalfd(*ch, s);
return s;
}

static char* fsiginfo(const struct signalfd_siginfo* const si, const size_t size, char buf[static const size]) {
FILE* const s = fmemopen(buf, size, "w");
setbuf(s, NULL);
xfprintf(s, "%s", strsignal(si->ssi_signo));
xfclose(s);
return buf;
}

static void process_signal(const struct signalfd_siginfo* const si) {
LOGDXP(char tmp[512], "signal: %s", fsiginfo(si, sizeof tmp, tmp));
}

int sighandler(struct sighandler_args*) {
int sfd = -1;
adjust_signal_delivery(&sfd);
struct signalfd_siginfo inf;
while (sus_read(sfd, &inf, sizeof inf) == sizeof inf)
process_signal(&inf);
return -1;
}
14 changes: 3 additions & 11 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@
v;\
})

#define xfprintf(...) XCALL(fprintf, __VA_ARGS__)
#define xsigprocmask(...) XCALL(xsigprocmask, __VA_ARGS__)
#define xselect(M, R, W, E, Timeout) XCALL(select, M, R, W, E, Timeout)
#define xread(...) XCALL(read, __VA_ARGS__)
#define xrread(...) XCALL(rread, __VA_ARGS__)
#define xwrite(...) XCALL(write, __VA_ARGS__)
#define xfclose(...) XCALL(fclose, true, __VA_ARGS__)
#define xfclose(...) XCALL(fclose, __VA_ARGS__)
#define xclose(...) XCALL(close, __VA_ARGS__)

#ifdef NDEBUG
Expand Down Expand Up @@ -150,16 +152,6 @@ int xsnprint_hex(size_t sbuf, input_t buf[static sbuf], size_t sbin, const byte_
NORETURN void exitb(const char* name);

inline static int syscall_exitf(const char* const name, const int ret) {
# ifndef NDEBUG
# if 0
if (strcmp(name, "select") == 0) {
if (iop->running_time_sec > 0)
assert(t.tv_sec < iop->running_time_sec);
else if (iop->running_time_sec == 0)
assert(t.tv_sec == 0);
}
# endif
# endif
if (ret == -1)
exitb(name);
return ret;
Expand Down

0 comments on commit 4b97526

Please sign in to comment.