Skip to content

Commit

Permalink
Small rework
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed Apr 27, 2024
1 parent 4b97526 commit 7f08ceb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
18 changes: 4 additions & 14 deletions sighandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,14 @@
# 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 int create_signalfd(void) {
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;
xsigprocmask(SIG_BLOCK, &s, NULL);
return xsignalfd(-1, &s, SFD_CLOEXEC);
}

static char* fsiginfo(const struct signalfd_siginfo* const si, const size_t size, char buf[static const size]) {
Expand All @@ -42,8 +33,7 @@ static void process_signal(const struct signalfd_siginfo* const si) {
}

int sighandler(struct sighandler_args*) {
int sfd = -1;
adjust_signal_delivery(&sfd);
const int sfd = create_signalfd();
struct signalfd_siginfo inf;
while (sus_read(sfd, &inf, sizeof inf) == sizeof inf)
process_signal(&inf);
Expand Down
3 changes: 2 additions & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
})

#define xfprintf(...) XCALL(fprintf, __VA_ARGS__)
#define xsigprocmask(...) XCALL(xsigprocmask, __VA_ARGS__)
#define xsigprocmask(...) XCALL(sigprocmask, __VA_ARGS__)
#define xsignalfd(...) XCALL(signalfd, __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__)
Expand Down

0 comments on commit 7f08ceb

Please sign in to comment.