diff --git a/Makefile b/Makefile index 1fcfc52..dc62a42 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ LDFLAGS ?= -flto LDFLAGS += $(call if_coverage,--coverage) # Project configuration ######################################################## -RL_C := wireprotocol.c header.c body.c attrs.c frame.c +RL_C := wireprotocol.c header.c body.c attrs.c frame.c frontend.c RL_O := $(RL_C:.c=.o) CFILES := main.c log.c utils.c serialization.c ucspi.c npthfix.c OFILES := $(RL_O) $(CFILES:.c=.o) diff --git a/frob.h b/frob.h index 685f9b9..d8070ba 100644 --- a/frob.h +++ b/frob.h @@ -288,6 +288,7 @@ static_assert(sizeof (struct frob_msg) % 16 == 0, "Message shall fit into 16-byt #pragma once struct fsm_frontend_foreign_args { + int in; int cs; }; @@ -300,7 +301,7 @@ struct fsm_frontend_timer_args { }; struct fsm_wireformat_args { - const int infd; + const int in, out; }; struct autoresponder_args { @@ -320,7 +321,7 @@ struct s6_notify_args { }; void* fsm_wireformat(const struct fsm_wireformat_args*); -int fsm_frontend_foreign(struct fsm_frontend_foreign_args*); +void* 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*); diff --git a/fsm/frontend.rl b/fsm/frontend.rl index 4648c01..8b09efb 100644 --- a/fsm/frontend.rl +++ b/fsm/frontend.rl @@ -1,10 +1,10 @@ -#include "multitasking/sus.h" #include "frob.h" #include "log.h" #include "utils.h" #include #include #include +#include static int cs; @@ -31,18 +31,18 @@ static int cs; acknak = 0x15; } action Send { - if (sus_write(6, &acknak, 1) != 1) { + LOGDXP(char tmp[4*1], "← % 4d %s", 1, PRETTY(&acknak, &acknak + 1, tmp)); + if (npth_write(6, &acknak, 1) != 1) { LOGE("write"); fbreak; } - LOGDXP(char tmp[4*1], "← % 4d %s", 1, PRETTY(&acknak, &acknak + 1, tmp)); } action Process { LOGDXP(char tmp[4*(pe-p)], "Lending %zd bytes: %s", pe - p, PRETTY((unsigned char*)p, (unsigned char*)pe, tmp)); - sus_lend(1, pe - p, (char*)p);// TODO: Remove this cast + //sus_lend(1, pe - p, (char*)p);// TODO: Remove this cast } action Forward { - if (sus_write(forwarded_fd, p, pe - p) != pe - p) { + if (npth_write(forwarded_fd, p, pe - p) != pe - p) { LOGE("write"); fbreak; } @@ -76,24 +76,22 @@ static int fsm_exec(const char* p, const char* const pe) { __attribute__((constructor)) void fsm_frontend_init() { (void)frontend_en_main, (void)frontend_error, (void)frontend_first_final; - set_nonblocking(6); %% write init; } -int fsm_frontend_foreign(struct fsm_frontend_foreign_args* const a) { +void* fsm_frontend_foreign(struct fsm_frontend_foreign_args* const a) { (void)a; ssize_t bytes; + char buf[1024]; const char* p; - while ((bytes = sus_borrow(0, (void**)&p)) >= 0) { + while ((bytes = npth_read(a->in, buf, sizeof buf)) >= 0) { LOGDX("Received %zd bytes", bytes); - const char* const pe = p + bytes; + const char* const pe = (p = buf) + bytes; fsm_exec(p, pe); - sus_return(0, p, bytes); } if (bytes < 0) LOGE("Closing fronted"); - sus_disable(1); - return -1; + return NULL; } int fsm_frontend_internal(struct fsm_frontend_internal_args* const a) { @@ -114,7 +112,7 @@ int fsm_frontend_timer(struct fsm_frontend_timer_args* const a) { const int fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); ssize_t bytes; unsigned char buf[8]; - while ((bytes = sus_read(fd, buf, sizeof buf)) > 0) { + while ((bytes = npth_read(fd, buf, sizeof buf)) > 0) { const char* p = (char[]){0}, * const pe = p + 1; fsm_exec(p, pe); } diff --git a/fsm/wireprotocol.rl b/fsm/wireprotocol.rl index 91d97b8..d9fdb81 100644 --- a/fsm/wireprotocol.rl +++ b/fsm/wireprotocol.rl @@ -18,9 +18,9 @@ } action LRC_Check { if (lrc != fc) { - //sus_lend(0, 1, "\x03"); + npth_write(args->out, "\x03", 1); } else { - //sus_lend(0, fpc - start, buf); + npth_write(args->out, buf, fpc - start); } } action Frame_Start { @@ -41,7 +41,7 @@ void* fsm_wireformat(const struct fsm_wireformat_args* const args) { int cs; unsigned char* p = buf, * pe = p; %% write init; - while ((bytes = npth_read(args->infd, buf, sizeof buf)) > 0) { + while ((bytes = npth_read(args->in, buf, sizeof buf)) > 0) { pe = (p = buf) + bytes; LOGDXP(char tmp[4*bytes], "→ % 4zd %s", bytes, PRETTY(p, pe, tmp)); %% write exec; diff --git a/main.c b/main.c index 0d2a3d4..aa92065 100644 --- a/main.c +++ b/main.c @@ -1,22 +1,10 @@ #include "log.h" #include "utils.h" #include "frob.h" +#include "npthfix.h" #include #include #include -#include - -typedef void* (* const npth_entry_t)(void*); - -struct ThreadBag { - npth_t handle; - const char* const name; - npth_entry_t entry; - void* const arg; -}; - -#define npth_define(Entry, ...) \ - { .entry = (npth_entry_t)Entry, .name = #Entry, .arg = &(struct Entry ## _args){ __VA_ARGS__ } } static void adjust_rlimit(void) { // This will force syscalls that allocate file descriptors to fail if it @@ -35,10 +23,13 @@ int main(const int ac, const char* av[static const ac]) { adjust_rlimit(); int fd_fo_main = STDOUT_FILENO, fd_fi_main = STDIN_FILENO; ucsp_info_and_adjust_fds(&fd_fo_main, &fd_fi_main); + int frontend_pipe[2]; + xpipe(frontend_pipe); struct ThreadBag thr[] = { - npth_define(fsm_wireformat, .infd = fd_fi_main) + npth_define(fsm_wireformat, .in = fd_fi_main, .out = frontend_pipe[1]), + npth_define(fsm_frontend_foreign, .in = frontend_pipe[0]) }; - npth_init(); + xnpth_init(); npth_sigev_init(); npth_sigev_add(SIGPWR); npth_sigev_fini(); @@ -48,7 +39,6 @@ int main(const int ac, const char* av[static const ac]) { } for (;;) { int sig; - LOGDX("Waiting for a signal..."); xnpth_sigwait(npth_sigev_sigmask(), &sig); switch (sig) { case SIGPWR: diff --git a/npthfix.c b/npthfix.c new file mode 100644 index 0000000..c2240af --- /dev/null +++ b/npthfix.c @@ -0,0 +1,8 @@ +#include "npthfix.h" + +int npth_sigwait(const sigset_t *set, int *sig) { + npth_unprotect(); + const int res = sigwait(set, sig); + npth_protect(); + return res; +} diff --git a/npthfix.h b/npthfix.h new file mode 100644 index 0000000..01e1076 --- /dev/null +++ b/npthfix.h @@ -0,0 +1,14 @@ +#pragma once +#include + +#define npth_define(Entry, ...) \ + { .entry = (npth_entry_t)Entry, .name = #Entry, .arg = &(struct Entry ## _args){ __VA_ARGS__ } } + +typedef void* (* const npth_entry_t)(void*); + +struct ThreadBag { + npth_t handle; + const char* const name; + npth_entry_t entry; + void* const arg; +}; diff --git a/utils.c b/utils.c index 2fd32bb..2029365 100644 --- a/utils.c +++ b/utils.c @@ -10,12 +10,12 @@ #include #include -void set_nonblocking(const int fd) { +void set_blocking(const int fd) { const int flags = fcntl(fd, F_GETFL, 0); if (flags == -1) EXITF("fcntl F_GETFL"); - if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) - EXITF("fcntl F_SETFL O_NONBLOCK"); + if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1) + EXITF("fcntl F_SETFL ~O_NONBLOCK"); } input_t calculate_lrc(input_t* p, const input_t* const pe) { diff --git a/utils.h b/utils.h index 3fac565..0c3f7f5 100644 --- a/utils.h +++ b/utils.h @@ -67,6 +67,7 @@ #define xnpth_setname_np(...) XCALL(npth_setname_np, __VA_ARGS__) #define xnpth_create(...) XCALL(npth_create, __VA_ARGS__) #define xnpth_sigwait(...) XCALL(npth_sigwait, __VA_ARGS__) +#define xnpth_init(...) XCALL(npth_init, __VA_ARGS__) #define xfprintf(...) XCALL(fprintf, __VA_ARGS__) #define xfputs(...) XCALL(fputs, __VA_ARGS__) #define xfflush(...) XCALL(fflush, __VA_ARGS__) @@ -78,6 +79,7 @@ #define xwrite(...) XCALL(write, __VA_ARGS__) #define xfclose(...) XCALL(fclose, __VA_ARGS__) #define xclose(...) XCALL(close, __VA_ARGS__) +#define xpipe(...) XCALL(pipe, __VA_ARGS__) #ifdef NDEBUG # define XCALL(Syscall, ...) syscall_exitf(#Syscall, Syscall(__VA_ARGS__)) @@ -102,7 +104,7 @@ typedef uint8_t input_t; -void set_nonblocking(int fd); +void set_blocking(int fd); byte_t hex2nibble(char h); byte_t unhex(const char h[static 2]);